Monday, June 9, 2014

Best practices when using Apache openJPA entity manager

We are using Apache openJPA excessively in Apache Airavata project. All the communication between data layer and frontend is done via OpneJPA. We recently found that we are facing some memory leaks with some openJPA classes. When we analyze the memory dump, below is the culprits that we got.

Problem Suspect 1
546,326 instances of "org.apache.openjpa.kernel.FinalizingBrokerImpl", loaded by "sun.misc.Launcher$AppClassLoader @ 0x7007c7bc0" occupy 1,031,525,848 (56.39%) bytes. These instances are referenced from one instance of "java.util.concurrent.ConcurrentHashMap$Segment[]", loaded by ""

Keywords
java.util.concurrent.ConcurrentHashMap$Segment[]
sun.misc.Launcher$AppClassLoader @ 0x7007c7bc0
org.apache.openjpa.kernel.FinalizingBrokerImpl

Problem Suspect 2
546,300 instances of "org.apache.openjpa.kernel.LocalManagedRuntime", loaded by "sun.misc.Launcher$AppClassLoader @ 0x7007c7bc0" occupy 680,034,240 (37.17%) bytes. These instances are referenced from one instance of "java.util.concurrent.ConcurrentHashMap$Segment[]", loaded by ""

Keywords
java.util.concurrent.ConcurrentHashMap$Segment[]
sun.misc.Launcher$AppClassLoader @ 0x7007c7bc0
org.apache.openjpa.kernel.LocalManagedRuntime

This is how we used OpneJPA entity manager when we are getting this exception. 



And below is the memory graph with the memory leak.


We wanted to fix this memory leak so badly since it took lot of memory of the system. With help of OpenJPA community, we get to know that we are not closing OpneJPA entity manager properly in the final block. Below is the correct way to close the entity manager.


After fixing all the places, we are able to solve the memory leak issue and below is the memory graph that we get after the fix.


As you can see, it is much better memory graph compare to previous one. 

No comments: