Search code examples
performancereflectioncollectionskotlingarbage

Kotlin: Massive amounts of ConsPStack, how can I avoid?


How can I avoid the massive amount of ConsPStack that are created by my application? When are these created? My application: https://github.com/Jire/Abendigo

enter image description here


Solution

  • UPDATE (2016-09-05): the issue has been fixed and is going to be available in Kotlin 1.0.5.

    Kotlin reflection implementation is using pcollections to cache KClass instances for different classes. Large amount of ConsPStack instances probably means you're using reflection on a lot of different classes. As I mentioned in another answer, reflection implementation is not optimized at all, so issues like this are somewhat expected at the moment.

    If this issue is really critical for you, I would recommend to cut down KClass creation by using Java reflection where possible. Also, as an extreme solution, you can try clearing the contents of the described cache at a particular moment. Here's the corresponding code in Kotlin project. Since the cache (var K_CLASS_CACHE) is private, you'd need to use Java reflection to get access to it.