This code can fail with OutOfMemory Exception when recNos array is large ( >20,000) when I commit the close session. I'm only reading information and only reading one object at a time (the idea was to reduce memory load by reading one at a time) and I only use the object for that invocation in the loop, but I'm not explicitly discarding it. Am I doing something wrong can I explicitly release the memory.
try
{
session = com.jthink.songlayer.hibernate.HibernateUtil.getSession();
for (Integer next : recNos)
{
Song song = SongCache.loadSongFromDatabase(session, next);
folderToSongIds.put(new File(song.getFilename()).getParent(),song.getRecNo());
}
}
finally
{
HibernateUtil.closeSession(session);
}
This is the stacktrace
java.lang.OutOfMemoryError: GC overhead limit exceeded
at org.hibernate.internal.util.collections.IdentityMap.entryArray(IdentityMap.java:165)
at org.hibernate.internal.util.collections.IdentityMap.concurrentEntries(IdentityMap.java:76)
at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:237)
at org.hibernate.internal.SessionImpl.cleanup(SessionImpl.java:651)
at org.hibernate.internal.SessionImpl.close(SessionImpl.java:363)
at com.jthink.songlayer.hibernate.HibernateUtil.closeSession(HibernateUtil.java:94)