Description
Hello,
We noticed on one of our production system that the ReflectionCache retains indirectly a lot of classes which adds up to a lot of memory.
In our case, after a full GC, we still have close to 2 millions kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf$Type
along with other internal kotlin reflect classes taking close to 1 GB on the heap.
Analysing a heap dump, the culprit seems to be javaConstructorToValueCreator which by retaining a list of KParameter
indirectly retains kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassConstructorDescriptor
which in turns has a lot of references to other classes.
I'm not knowledgeable enough with kotlin reflection to suggest a solution there. But we could wrap the values of all the maps inside ReflectionCache with SoftReference
to let the GC collect them when needed.
Happy to submit a PR if you agree with this solution.
Thank you
Versions
Kotlin: 1.5.31
Jackson-module-kotlin: 2.13.3
Jackson-databind: 2.13.3
Additional context
I link a screenshot of Eclipse Memory Analyzer which show the merged shortest path excluding any soft/weak references.