javahibernateheap-dumpout-of-memory

OOM (Java heap) error due to org.postgresql.core.Field


I have a Java application using Hibernate to connect to a PostgreSQL backend. Been getting java.lang.OutOfMemoryError: Java heap space error very frequently. Upon analysing the heap dump on OOM and comparing it with a heap dump taken during a normal run I noticed a significant increase in org.postgresql.core.Field objects. What could be the potential reason for this? Could it be due to improper closure of DB connections by Hibernate?

enter image description here

Jdk: 18

DB: Postgresql (42.3.5)

Hikaricp: 4.0.3

Spring boot: v2.7.0


Solution

  • org.postgresql.core.Field is a class from the postgresql driver. If you have these instances hanging around it seems that you are not freeing up jdbc resources.

    In plain jdbc this would likely happen if you weren't calling ResultSet.close(), Statement.close() and Connection.close() to free up resources.

    With hibernate these are wrapped via the hibernate Session so you need to ensure that you are freeing up Session resources once you are done with them.

    How are you managing your hibernate Sessions? Are you returning / closing resources once you are done with them?

    From the hibernate session javadocs

    A typical transaction should use the following idiom:

    
     Session sess = factory.openSession();
     Transaction tx;
     try {
         tx = sess.beginTransaction();
         //do some work
         ...
         tx.commit();
     }
     catch (Exception e) {
         if (tx!=null) tx.rollback();
         throw e;
     }
     finally {
         sess.close();
     }