Paul Taylor
Paul Taylor

Reputation: 13120

Does hibernate session Evict method free up memory resources immediatel;y

My application uses hibernate, when creating a report I get a hibernate session, load each widget from database one by one extract the info I need then evict the item from the session.

Although this might not be the most perfomant I do it this way because there maybe many widgets and I could run out of memory if I tried to load too many widgets in one go. My understanding of evict() would that any resources associated with the item would be removed from the hibernate session. And because the song field is then associated with the next record the only thing that cannot be garbage collected is the folderToSongIds map

However I still sometimes see OutofMemory error in this code when recNos is large.

I suppose this could simply be because of the size of recNos/folderToSongIds variables but they dont store much large objects so I want to check that evict() does what I want and to see if there is anything else I can do to use less memory.

 try
        {
            session = com.jthink.songlayer.hibernate.HibernateUtil.getSession();

            //For each song group by its parent
            for (Integer next : recNos)
            {


                //Group songs changed by parent folder
                Song song = SongCache.loadSongFromDatabase(session, next);
                File file = new File(song.getFilename());
                folderToSongIds.put(file.getParent(), song.getRecNo());
                session.evict(song);
            }
        }
        catch (Throwable t)
        {
            Errors.addError("Unable to do complete Report:"+t.getMessage());
        }
        finally
        {
            HibernateUtil.closeSession(session);
        }

Upvotes: 0

Views: 524

Answers (1)

Thierry
Thierry

Reputation: 5440

Evict will indeed remove the entity from the hibernate Session. From the code you pasted, it will eventually be garbage-collected.

Without more info, you won't be able to fix your outOfMemory errors. Start the VM with these options : -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=_change_me_

When an OutOfMemoryError happens, analyze the memory dump (for example with eclipse memory analyzer) and see what is using so much RAM.

Upvotes: 2

Related Questions