-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Excessive memory usage when using KotlinModule with Kotlin data class #774
Comments
First, most of the memory consumption comes from
The analysis required for |
Thank you very much for looking into the issue. I am a user of Please note that the issue is not about memory usage, but about allocations. The problem is precisely that a lot of memory is allocated (and then freed), which is causing memory overhead, which causes the garbage collector to perform work. It may well be that there is no bug at all, but perhaps there are allocations that are performed each time during deserialization that could be (optionally) cached to lower the allocation overhead (with the cost of more persistent memory usage). Especially when using reflection on types, it seems wasteful to perform the reflection each time. Perhaps there is a technique I can use already to avoid the reflection and the associated memory overhead.
The reproducible case that I posted shows that using a Kotlin Please let me know if you require any more information. |
Search before asking
Describe the bug
When using
KotlinModule
with anObjectMapper
to unmarshall a Kotlindata class
, it uses a lot more memory than when not usingKotlinModule
or when not using a Kotlindata class
(e.g. when using a Javarecord
).To Reproduce
Please see: https://github.com/t9t/jackson-kotlin-memory
Expected behavior
Lower memory overhead when reading Kotlin data classes. Especially when using Jackson annotation.
Versions
Kotlin: 1.9.22
Jackson-module-kotlin: 2.16.1
Jackson-databind: 2.16.1
Additional context
We found this out in a backend message processing app, which was crashing due to high CPU usage spend in the garbage collector. Upon inspection, we were parsing JSON payloads in the 10KiB-20KiB range (hundreds per second), and generating dozens of MiBs of garbage per second.
When playing with a profiler locally, I noticed a lot of allocations in Jackson Kotlin code. I was able to distill a small test project that shows the issue: https://github.com/t9t/jackson-kotlin-memory
There is approximately 2x the memory overhead when using
KotlinModule
with a Kotlindata class
vs not usingKotlinModule
(with one of our specific 20 KiB payloads, the overhead withKotlinModule
vs without was 4x).The most surprising thing for me is that adding the Jackson annotations (
@JsonCreator
and@JsonProperty
) did not help with the issue. When I use a profiler, I see many allocations in Kotlin reflection functions (but I have to confess I'm yet a novice in using profilers). I would expect that when using the annotations, no reflection would be needed.The text was updated successfully, but these errors were encountered: