Return to site

Java Metaspace Memory Leak

broken image


Last Friday we had an Out Of Memory exception on one of our production WebLogic servers. The server only has 2 applications running on it, so at least the job of narrowing down the culprit would be easy. Or so we thought (note to self: find a better application monitoring/profiling solution!).

Cubase le torrent. A couple of years ago, there was an article on the Confluence site of the Spring framework written by Magnus Alvestad, titled 'Memory leaks where the classloader cannot be garbage collected' (originally located here / here). What is a Memory Leak in Java A memory leak is caused when the program fails to release unused memory which can lead to either unexpected results or application crash. Even though there is no official definition of a memory leak in Java we can broadly divide it.

Use the Java Flight Recordings to identify the memory leak. After your recording showing the leak, you can look at the Object Statistics. Look at one long recording, then look at which classes grew the most in heap usage over the recording. Also, there were obviously no classloader leaks (MetaSpace size was constant at around 130M). ByteBuffers looked good. We didn't use off-heap memory. Maybe garbage collection goes wild? First try: Native Memory Tracking Out of the box, Java offers Native Memory Tracking (NMT). Simply add at startup of your java application. Melda autopitch. Here is what the test script + code does: Loads jboss-marshalling 2.0.7.Final using a URLClassLoader and causes some SerializableClass instances to get cached Closes the URLClassLoader Repeats the previous two steps until the JVM runs out of metaspace (227 iterations when I ran it) The script then waits for the user to hit enter and then re.


6 hours and many cups of coffee later, I realized that the developer that had written this application was using JAXB to marshal / un-marshal XML from a web service end point. As per the API example, the developer was creating a JAXB context for every message in this manner:

Java Metaspace Out Of Memory

A new JAXB context was getting created every single time a web service message was sent/received! There is a known memory leak issue when using the JAXB context in this manner. Check out the While One Fork blog for more information.

Java Metaspace Memory Leaks


I ended up creating a JAXBContextFactory class that allows the JAXBContext to be cached and reused. Find the code below. We redeployed the code and voila - the memory issues disappeared!

Java Metaspace Memory Leak Foam

Java Metaspace Memory Leak

Java Metaspace Memory Leak Detection

Here is how to use it in your application:



broken image