FasterXML / FasterXML/jackson-module-kotlin

Excessive memory usage when using KotlinModule with Kotlin data class

Open
#774 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Kotlin
Stars
1.2k
Forks
187
Avg merge
9h 48m
Merged PRs (30d)
16

Description

### Search before asking

- [X] I searched in the [issues](https://github.com/FasterXML/jackson-module-kotlin/issues) and found nothing similar.
- [X] I searched in the [issues of databind](https://github.com/FasterXML/jackson-databind/issues) and other modules used and found nothing similar.
- [X] I have confirmed that the problem only occurs when using Kotlin.

### Describe the bug

When using `KotlinModule` with an `ObjectMapper` to unmarshall a Kotlin `data class`, it uses a lot more memory than when not using `KotlinModule` or when not using a Kotlin `data class` (e.g. when using a Java `record`).

### 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 Kotlin `data class` vs not using `KotlinModule` (with one of our specific 20 KiB payloads, the overhead with `KotlinModule` 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.