Kotlin: release KotlinCoreEnvironment on every parse path#7705
Merged
Conversation
`KotlinParser.parseInputs` deferred `Disposer.dispose(disposable)` to a
tail `Stream.generate(...).limit(1)` element, so the disposable only ran
when the caller iterated past every parsed source. `JavaTemplateParser`'s
template-stub path consumes the parser stream via `.findFirst()`, which
short-circuits before the dispose element is reached, leaking one
`KotlinCoreEnvironment` per `KotlinTemplate.apply()` call. Each leaked
environment retains the full Kotlin/JDK classpath `ProtoBuf` metadata
(~25MB on a typical recipe-module classpath), so heavy template-driven
suites OOM well below their nominal heap ceiling.
Materialize all source files inside `try { … } finally { Disposer.dispose(…); }`
and return a stream over the materialized list. The existing `parse(…)`
already built every `CompiledSource` eagerly before the stream produced
any element, so the change loses no streaming property — it just makes
the cleanup unconditional.
Measured on a downstream Kotlin-recipe test suite (189 tests, ~200
recipe-driven template applies): pre-fix the suite OOM'd at `-Xmx6g`;
post-fix it passes at the Gradle default `-Xmx512m` in ~30s.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KotlinParser.parseInputsdeferredDisposer.dispose(disposable)to a tailStream.generate(...).limit(1)element, so the disposable only ran when the caller iterated past every parsed source.JavaTemplateParser's template-stub path consumes the parser stream via.findFirst(), which short-circuits before the dispose element is reached, leaking oneKotlinCoreEnvironmentperKotlinTemplate.apply()call. Each leaked environment retains the full Kotlin/JDK classpathProtoBufmetadata (~25MB on a typical recipe-module classpath), so heavy template-driven suites OOM well below their nominal heap ceiling.Fix
Materialize all source files inside
try { … } finally { Disposer.dispose(…); }and return a stream over the materialized list. The existingparse(…)already built everyCompiledSourceeagerly before the stream produced any element, so the change loses no streaming property — it just makes the cleanup unconditional.Diagnostic evidence
Captured via
jcmd GC.class_histogramsnapshots while a downstream Kotlin-recipe suite (189 tests, ~200 template applies) ran at-Xmx2g:KotlinCoreEnvironmentliveProtoBuf$Typelive71 × ~25MB ≈ 1.8GB, matching the heap at OOM. With the fix, the same suite passes at the Gradle default
-Xmx512min ~30s.Test plan
./gradlew :rewrite-kotlin:test— 1205 / 1205 greenKotlin1To2Test, 189 tests) passes at default heap (was OOMing at 6g)