Search code examples
javamemoryapache-kafkacpuspring-kafka

Java Memory Leak on creating many empty Maps


It is possible that during receiving the messages from Kafka, when I'm creating empty map using Map.of() I can cause a memory leak or more intensive CPU usage?

There can be multiple listeners, but there are two types - one that receive headers:

    @KafkaListener(...)
    public void processMessage(String message,
                               ConsumerRecordMetadata metadata,
                               @Header(name = ID, required = false) String id,
                               @Header(name = TIME, required = false) String time) {
        var headers = Map.of(ID, id, TIME, time);
        ...
}

and another, where there are no headers:

    @KafkaListener(...)
    public void processMessage(String message,
                               ConsumerRecordMetadata metadata) {
        var headers = Map.of();
        ...
}

And then in more abstract level I'm checking if there are headers or not, but I think it's not the case - so as in the title, is there a possibility that if the messages traffic increases the CPU could be throttled?

And yes, this is happening :|


Solution

  • There is no reason to believe that an empty Map could lead to memory leak more than one which holds data.

    Leaking memory does not directly cause cpu throttling either (using up all your heap can, so leaks can eventually lead to that).

    You should assess the problem before jumping to a conclusion.

    Check GC statistic to see if your memory is ever increasing, or if you have other issues with GC cycles, then you can try to identify the cause of the issue.