FYI it looks like this strongly affects KSP too <https://youtrack.jetbrains.com/issue/KT-72169/Kotli...
z
k
Yup! I added my reproduction to that issue as well. Seems that use of KSP is the main contributor of class loader leaks in the Kotlin Compile Daemon.
Paul Klauser isn't in this Slack yet but he's trying to join
👋 1
z
It looks like this is actually specific to KSP: https://github.com/google/ksp/issues/2073#issuecomment-2406006495 CC @Ting-Yuan Huang
👀 1
t
Thanks for letting know. I'll investigate asap.
🙌 1
k
Looked into the usage of ThreadLocals in KSP and the only 2 that seem to need clearing are the ones pointed out by Brian in
KSObjectCacheManager
. I have a branch here with the changes to remove them and all tests still pass. I want to validate that this actually addresses the memory leaks we were able to reproduce with NowInAndroid, but I'm running into issues with the metadata not matching Kotlin releases. I'm trying to now use
2.1.0-dev-5441
but unable to find the maven repo where this is hosted. Do I just need to build it from source for my local maven repo?
Copy code
Plugin [id: 'org.jetbrains.kotlin.plugin.compose', version: '2.1.0-dev-5441', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Included Builds (No included builds contain this plugin)
- Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.1.0-dev-5441')
  Searched in the following repositories:
    Google
    MavenRepo
    MavenLocal(file:/Users/jason/.m2/repository/)
    Gradle Central Plugin Repository
    compose-compiler(<https://androidx.dev/storage/compose-compiler/repository/>)
    Compose-JB(<https://maven.pkg.jetbrains.space/public/p/compose/dev>)
Found the repo here thanks to Kotlin source in gradle.properties pointing out the bootstrap repo
Well, I'm a bit further but now seem to need to figure out how build Kotlin metadata properly
Copy code
> Task :app:hiltJavaCompileDemoDebug FAILED
ASM Instrumentation process wasn't able to resolve some classes, this means that
the instrumented classes might contain corrupt stack frames. Make sure the
dependencies that contain these classes are on the runtime or the provided
classpath. Otherwise, the jvm might fail to load the corrupt classes at runtime
when running in a jvm environment like unit tests.

Classes that weren't resolved:
> androidx.window.extensions.embedding.ActivityEmbeddingComponent
> androidx.window.extensions.layout.WindowLayoutComponent
> androidx.window.sidecar.SidecarDeviceState
> androidx.window.sidecar.SidecarInterface
> androidx.compose.animation.tooling.ComposeAnimatedProperty
> com.google.common.util.concurrent.ListenableFuture


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:hiltJavaCompileDemoDebug'.
> java.lang.IllegalStateException: Unable to read Kotlin metadata due to unsupported metadata version.
Looks like Bazel doesn't support SNAPSHOT sources since the maven install requires a lockfile and that means it will never resolve a snapshot...
(building Dagger from source against local KSP build - this is a fun saturday morning)\
I've gotten non-snapshot publishing to my local Maven repo ... and now pretty sure Bazel just wants to see checksums for the artifact. Looking through Maven documentation I'm a bit lost on how these get generated unless I use the maven plugin for creating them, doesn't appear to be anything for
mvn
cli tool.
Bazel does not make it easy to use bootstrap dev Kotlin
I put up a change that likely is the fix, if someone else can clue me in on how to use dev kotlin bootstrap compiler in Bazel build for Dagger I'd be able to verify whether or not it really does fix the issue. https://github.com/google/ksp/pull/2146
z
@kaeawc thanks for validating your change fixed things too! Looks like a big win for a simple fix 🚀
t
Thank you so much @kaeawc! The patch looks good to me and I'll merge it today so that the nightly build of KSP, which is on stable Kotlin 2.0.21, will have it. Hopefully you won't have to deal with bootstrap Kotlin then.
🙌 3
♥️ 1
k
Amazing! Glad to help, was a good learning experience
🙏 1
🙏 1
Trying out the KSP snapshot repo and I'm not able to actually consume it. Is there an example handy somewhere? Maybe I'm just missing something obvious. Strange thing is all the sources are there and they seem to match my local maven. I'll do a diff next to verify that, but thought I'd ask before going further