Tag Archives: java

Ehcache statistics

Here is what you need to do if you want to enable useful statistics information for the ehcache implementation in your Java application:

  1. Define two new beans – one for ehcacheManagementService and the other for mbeanServer – in the applicationContext file. Make sure that registerCacheStatistics constructor argument is set to true.
    <bean id="ehcacheManagementService" class="net.sf.ehcache.management.ManagementService">
          <constructor-arg name="cacheManager" ref="ehcache"/>
          <constructor-arg name="mBeanServer" ref="mbeanServer"/>
          <constructor-arg name="registerCacheManager" value="false"/>
          <constructor-arg name="registerCaches" value="false"/>
          <constructor-arg name="registerCacheConfigurations" value="false"/>
          <constructor-arg name="registerCacheStatistics" value="true"/>
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
          <property name="locateExistingServerIfPossible" value="true"/>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache"/>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:configLocation="classpath:ehcache.xml" p:shared="true"/>
  2. Set statistics property of your cache in ehcache.xml to true.
    <cache name="coreCache"

That’s it! Ehcache mbeans are now exposed to be consumed by any JMX-compatible monitoring tool, viz., JConsole or VisualVM (with Java Profiler plugin) to name a few.

Here is a screenshot of the Ehcache statistics in VisualVM:

Screen Shot 2014-08-08 at 23.33.18

Maven Surefire PermGen error

In case you are getting PermGen error while running tests using the Maven Surefire plugin, here is the solution:

    <argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>

Setting the JVM args as part of the plugin did the trick!

Setting MAVEN_OPTS would not help because Maven Surefire plugin forks the JVM process while running the tests, and so the MAVEN_OPTS settings do not take effect.