Java Chatter and Random Nagging

Wednesday, January 30, 2008

Reduce your memory usage with Hibernate's evict

I was running low on memory when performing an operation for all Employee objects of my Company object. Since the operation performed changes on a big hashmap, deep inside my Employee object, I noticed that the operation on subsequent employees took more and more time, until the application finally ran out of memory.

I fixed this by fetching a hibernate iterator and evicting my Employee object when my operation was done.


Iterator userIterator = userRepository.getUserIterator(company);
while(userIterator.hasNext()){
   User user = userIterator.next();
   user.getEmployee().openYearForBookings(holidayYear);
   userRepository.flush();
   userRepository.evict(user);
}


Sorry for the flush, but when not flushing the session before the evict, I lost all changes made to the evicted object.

2 Comments:

At 5:18 pm, Anonymous Anonymous said...

hi nice website there is another website helpfull as you that called www.my-dailyads.com

 
At 1:18 am, Blogger Unknown said...

The reason why the memory usage increase is that the memory was not released.

Close your Hibernate session after the transaction (or before you are not going to use the session) to make sure no memory leakage.

 

Post a Comment

<< Home