Skip to content

Metrics objects are not cleaned up when rebuilding context, eventually resulting in OutOfMemoryError #973

@mcanalesmayo

Description

@mcanalesmayo

Expected Behavior

If a Micronaut test is annotated like @MicronautTest(rebuildContext = true), I'd expect everything to be cleaned up including the registered metrics objects (when using Micronaut Micrometer integration).

Actual Behaviour

It seems like metrics objects are not cleaned up, while new ones are created for each test every time the context is rebuilt, eventually resulting in java.lang.OutOfMemoryError.

Existing workaround is to disable metrics if they are not needed by the tests, via micronaut.metrics.binders.kafka.enabled or micronaut.metrics.binders.kafka.streams.enabled.

Steps To Reproduce

We have a Kafka Streams project with 70+ tests that require rebuildContext = true due to the use of @Requires and @Property (as documented here). For each test, different Kafka streams are created.

Eventually, it hangs and results in OutOfMemoryError in some application thread (unrelated to Kafka streams). We took a heap dump on out of memory error and analyzed it with VisualVM. We can observe the number of KafkaMetricMeterType object is quite high, which suggests that they are not cleaned up every time context is rebuilt.

Please, see screenshot below.

Screenshot 2024-03-12 at 20 14 50

Environment Information

JDK 21

Example Application

No response

Version

4.3.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions