-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Quey.list cause memory leak (OutOfMemoryError)
PostPosted: Sat Jun 14, 2008 2:50 pm 
Newbie

Joined: Tue May 13, 2008 8:08 am
Posts: 4
Hi,

My problem doesn't solved.

I run a client program that load each iterate 300 hibernate objects using Query.list(). Unfortunatly, after a while, I got OutOfMemoryError.
I ran it many times and it always fails after around 2400 iterations.
I watched it through JProfiler and I saw that the used heap size growes significantly during this time and the GC couldn't release enough memory.

The sequence of the process is (300 entities in each iteration):
1. Load the entities (relevant code attached below)
2. Do some business
3. Update the entities

I use Jboss 3.2 and Hibernate3.
1. I'm using CMT(container managed transactions) therfore ther is no transactions in the code.


The hibernate code:
Session session = null;

try {

session = openSession();

Query query = prepareHQLQuery(session, hqlQuery, boundedVariables);

return query.list();

} catch (HibernateException hibEx) {
logger.error("Failed to get list of entities", hibEx);
throw new PersistencyException("Failed to get list of entities");
} finally {
closeSession(session);
}


code of the update:

Session session = null;
PersistentEntity entity = null;

try {
session = openSession();
} catch (HibernateException hiberEx) {
logger.error("Failed to open session", hiberEx);
throw new PersistencyException();
}

for (int i=0;i<entities.length;i++) {
entity = entities[i];

try {

session.update(entity);


} catch (HibernateException hibEx) {
logger.error("Failed to update entity with ID "
+ entity.getId().longValue() + " at the storage", hibEx);

if (!proceedOnError) {

try {
session.flush();

closeSession(session);
} catch (Exception ex) {
logger.warn("Failed to flush connection", ex);
}

try {
closeSession(session);
} catch (Exception ex) {
logger.warn("Failed to close connection to storage", ex);
}

throw new PersistencyException("failed to update Entity of type "
+ entity.getClass().getName());

}

}
}

try {

session.flush();

} catch (HibernateException hiberEx) {
logger.error("Failed to commit changes while updating entities", hiberEx);
throw new PersistencyException();
} finally {
closeSession(session);
}


As I wrote, through the JProfiler I saw that query.list() method doesn't release the memory.

What is the trap in my code??

Thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 2:51 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you're doing everything in the same transaction the first-level cache will keep a reference to every object loaded by your query.

This page on batch processing shows how to avoid out of memory errors by using the session.clear() method to release objects from the first-level cache.

http://www.hibernate.org/hib_docs/reference/en/html/batch.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 10:58 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Take a look at the JavaDoc for the Hibernate org.hibernate Session:


Quote:
clear

public void clear()

Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults.


http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#clear()

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 1:21 am 
Newbie

Joined: Tue May 13, 2008 8:08 am
Posts: 4
I must say that during my tests I try the fillowing advices:

1. session.setCacheMode(CacheMode.IGNORE) before loading
2. session.clear() after I loaded the entities.
3. sessionFactory.evict(Class myLoadedClasses)
4. sessionFactory.evictQueries()


No change and no progress! It always failes around the same amount


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.