OpenJPA: Memory Leak Case Study
Join the DZone community and get the full member experience.
Join For Free- Java EE server: Oracle Weblogic Portal 10.0
- OS: Solaris 10
- JDK: Oracle/Sun HotSpot JVM 1.5 32-bit @2 GB capacity
- Java Persistence API: Apache OpenJPA 1.0.x (JPA 1.0 specifications)
- RDBMS: Oracle 10g
- Platform type: Web Portal
- Quest Foglight for Java (Java heap monitoring)
- MAT (Java heap dump analysis)
- Production outages were observed on regular basis after ~2 weeks of traffic.
- The failures were due to Java heap (OldGen) depletion e.g. OutOfMemoryError: Java heap space error found in the Weblogic logs.
- A Java heap memory leak was confirmed after reviewing the Java heap OldGen space utilization over time from Foglight monitoring tool along with the Java verbose GC historical data.
** A video explaining the following JVM Heap Dump analysis is now available here.
public class Application { @Resource private UserTransaction utx = null; // Initialized on each application request and not closed! @PersistenceUnit(unitName = "UnitName") private EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnit"); public EntityManager getEntityManager() { return this.emf.createEntityManager(); } public void businessMethod() { // Create a new EntityManager instance via from the newly created EntityManagerFactory instance // Do something... // Close the EntityManager instance } }
- Create and maintain only one static instance of javax.persistence.EntityManagerFactory per application class loader and implemented via the Singleton Pattern.
- Create and dispose new instances of EntityManager for each application request.
Published at DZone with permission of Pierre - Hugues Charbonneau, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments