Stefan’s Kugel

December 13, 2008

Memory Leak in Java

Filed under: Java — Tags: , , , — root @ 6:51 am

Java has by definition no memory leaks [think automatic garbage collection]. What leaks memory is running software, especially long running enterprise software, such as that on a web application server.

The picture below shows a memory leak of a Tomcat web application that reads from ActiveMQ messaging system and does a bulk insert into a database. The memory graph, snapshot from the JMX console, rises over 9 hours close to 800 MB before it throws periodic OutOfHeapSpace exceptions. The Tomcat instance was run with -Xmx1024m. Tomcat uses the rest for itself. This measurement was taken under the conditions of a stress test; it levels out at 800 MB after 9 hours. In production, where JMX is turned off, the OutOfHeapSpace error took a week or so to show up.

Memory Profile of a web application in JMX
How to diagnose a memory leak

  • Turn on JMX
  • Run it under stress conditions
  • Try to force garbage collection, see the “Perform GC” button on JMX console: if the graph doesn’t go down, it’s a leak!
  • How to find the root of the leak

  • Here’s a good writeup of what to do: http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/
  • Java 6 SE has all tools necessary: JMX, hprof agent, jmap, jhat
  • Taking a memory dump at different times [sorry the language] should result in one type of object growing in number
  • The most frequent object [except for chars in Strings probably] in jhat is a likely candidate for the leak.
  • In my case, the leak was caused by a JDBC database driver, JConn v6, for reasons I don’t really bother to know. Replacing that JDBC driver with the open-source jDTS driver for Sybase and SQLServer brought down memory consumption to 20MB and made it run faster. A nagging problem was solved.

    2 Comments »

    1. ?????????, ??? ????? ?? ???? ??????. ???????, ??? ????? ???????. ?? ????????????….

      ?????????? The picture below shows a memory leak of a Tomcat web application that reads from ActiveMQ messaging system and does a bulk insert into a database. […….

      Trackback by Kylie Batt — May 4, 2010 @ 11:44 am

    2. ? ???? ???-?? ????. ??????? ?? ?????? ? ???? ???????. ? ?? ???? ?????….

      ??????? ????????? ??????? ?????? 1 ????????? ??????? Java has by definition no memory leaks [think automatic garbage collection]…..

      Trackback by Kylie Batt — May 19, 2010 @ 1:30 pm

    RSS feed for comments on this post. TrackBack URL

    Leave a comment

    Powered by WordPress