On OOM in Kotlin daemon also show message how to increase heap size

^KT-58320 Fixed
This commit is contained in:
Yahor Berdnikau
2023-05-03 18:57:05 +02:00
committed by Space Team
parent 73cc73115e
commit 82786fe6bf
3 changed files with 27 additions and 14 deletions
@@ -114,10 +114,8 @@ class KotlinDaemonIT : KGPDaemonsBaseTest() {
)
buildAndFail("assemble") {
// 'assertOutputContains' is not suitable here as test produces OOM which TC will false-positively treat as
// whole configuration error. So we should not output test logs into build logs.
assert(output.contains("Not enough memory to run compilation. Try to increase it via 'gradle.properties':"))
assert(output.contains("kotlin.daemon.jvmargs=-Xmx<size>"))
assertOutputContains("Not enough memory to run compilation. Try to increase it via 'gradle.properties':")
assertOutputContains("kotlin.daemon.jvmargs=-Xmx<size>")
}
}
}
@@ -17,9 +17,10 @@ import org.jetbrains.kotlin.gradle.plugin.internal.state.TaskExecutionResults
import org.jetbrains.kotlin.gradle.plugin.internal.state.TaskLoggers
import org.jetbrains.kotlin.build.report.statistics.StatTag
import org.jetbrains.kotlin.gradle.report.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.OOMErrorException
import org.jetbrains.kotlin.gradle.tasks.cleanOutputsAndLocalState
import org.jetbrains.kotlin.gradle.tasks.throwExceptionIfCompilationFailed
import org.jetbrains.kotlin.gradle.tasks.kotlinDaemonOOMHelperMessage
import org.jetbrains.kotlin.gradle.utils.stackTraceAsString
import org.jetbrains.kotlin.incremental.ChangedFiles
import org.jetbrains.kotlin.incremental.ClasspathChanges
@@ -246,7 +247,11 @@ internal class GradleKotlinCompilerWork @Inject constructor(
exitCodeFromProcessExitCode(log, res.get())
} catch (e: Throwable) {
bufferingMessageCollector.flush(messageCollector)
throw e
if (e is OutOfMemoryError || e.hasOOMCause()) {
throw OOMErrorException(kotlinDaemonOOMHelperMessage)
} else {
throw e
}
} finally {
val memoryUsageAfterBuild = daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get()
@@ -23,13 +23,10 @@ fun throwExceptionIfCompilationFailed(
ExitCode.INTERNAL_ERROR -> throw FailedCompilationException("Internal compiler error. See log for more details")
ExitCode.SCRIPT_EXECUTION_ERROR -> throw FailedCompilationException("Script execution error. See log for more details")
ExitCode.OOM_ERROR -> {
var exceptionMessage = "Not enough memory to run compilation."
when (executionStrategy) {
KotlinCompilerExecutionStrategy.DAEMON ->
exceptionMessage += " Try to increase it via 'gradle.properties':\nkotlin.daemon.jvmargs=-Xmx<size>"
KotlinCompilerExecutionStrategy.IN_PROCESS ->
exceptionMessage += " Try to increase it via 'gradle.properties':\norg.gradle.jvmargs=-Xmx<size>"
KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> Unit
val exceptionMessage = when (executionStrategy) {
KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage
KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage
KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage
}
throw OOMErrorException(exceptionMessage)
}
@@ -38,6 +35,19 @@ fun throwExceptionIfCompilationFailed(
}
}
internal const val kotlinDaemonOOMHelperMessage = "Not enough memory to run compilation. " +
"Try to increase it via 'gradle.properties':\nkotlin.daemon.jvmargs=-Xmx<size>"
internal const val kotlinInProcessOOMHelperMessage = "Not enough memory to run compilation. " +
" Try to increase it via 'gradle.properties':\norg.gradle.jvmargs=-Xmx<size>"
internal const val kotlinOutOfProcessOOMHelperMessage = "Not enough memory to run compilation."
internal fun Throwable.hasOOMCause(): Boolean = when (cause) {
is OutOfMemoryError -> true
else -> cause?.hasOOMCause() ?: false
}
/** Exception thrown when [ExitCode] != [ExitCode.OK]. */
internal open class FailedCompilationException(message: String) : RuntimeException(message)