Description
In what version(s) of Spring for Apache Kafka are you seeing this issue?
- Spring Boot: 3.4.5
- Spring Kafka: 3.3.5
- Kafka Clients: 3.8.1
Describe the bug
After upgrading from Spring Kafka 3.1.1 to 3.3.5, we observed unexpected memory behavior when configuring a consumer using a regular expression that matches a large number of topics and partitions in the Kafka broker.
The application starts consuming memory progressively until it eventually fails with a java.lang.OutOfMemoryError: Java heap space.
This behavior was not present in version 3.1.1 with the same configuration and number of topics.
To Reproduce
- Set up a Spring Boot project with Spring Kafka 3.3.5 and Kafka Clients 3.8.1.
- Configure a consumer with destinationIsPattern: true using a regular expression that matches a large number of topics and partitions (e.g., ^(tp|TP).tracing-topic.(?!.-dlt$).$).
- Consume messages from the topics matching the pattern.
- Monitor the application’s memory usage over an extended period.
- Observe a progressive heap growth leading to a java.lang.OutOfMemoryError: Java heap space.
- Analyze the heap dump to identify retention of multiple instances related to Kafka consumers and containers.
Expected behavior
When using a topic pattern with destinationIsPattern: true, Spring Kafka is expected to efficiently manage the consumption of all matching topics without causing a progressive and uncontrolled increase in memory usage. Consumption should remain stable and heap usage should stay constant, similar to the behavior observed in version 3.1.1.
Sample