TL;DR Documents a severe memory leak issue with ReentrantLock and PooledConnection, located via jmap monitoring, resolved by upgrading to 5.7 or replacing with Spring connection pool.

Problem Symptoms

Recently, while using ActiveMQ’s connection pool, I discovered a very serious memory leak issue.

Investigation Process

Through jmap monitoring, it can be seen that java.util.concurrent.locks.ReentrantLock and org.apache.activemq.pool.PooledConnection occupy a very large amount of memory, and the growth rate is also very fast.

Root Cause Analysis

After searching online, I found exactly the corresponding ActiveMQ bug report: https://issues.apache.org/jira/browse/AMQ-3997

Solution

This bug has been fixed in version 5.7, so it can be resolved by upgrading the version.

At the same time, there is another solution, which is to use Spring’s connection pool to replace ActiveMQ’s built-in connection pool. The configuration is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://205-amq-broker2?create=false&amp;waitForStart=10000" />
</bean>

<!-- <bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>-->
<bean id="cachedConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsConnectionFactory"></property>
<property name="sessionCacheSize" value="10"></property>
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="cachedConnectionFactory"/>
<property name="concurrentConsumers" value="10"/>
</bean>

<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>

Source: https://lichuanyang.top/en/posts/13925/