-->
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.  [ 6 posts ] 
Author Message
 Post subject: Quey.list cause memory leak (OutOfMemoryError)
PostPosted: Tue May 13, 2008 9:12 am 
Newbie

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

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.

I use Jboss 3.2 and Hibernate3.

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);
}

thanks.


Top
 Profile  
 
 Post subject: OutOfMemory
PostPosted: Mon May 19, 2008 4:32 pm 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
You probably need to session.flush after n inserts or after every insert/update.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 5:44 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, you should always use transaction, even just for reading.

Also, could we see were you iterate in the "list()", do you open and close the session outside the loop or at each iteration?

The session keeps a reference to the objects it loaded until it is closed or cleared, so if you are doing much "batch" jobs you should use .flush() and .clear() periodically.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 3:48 am 
Newbie

Joined: Wed May 21, 2008 3:33 am
Posts: 1
Hi,
If you want to see who is keeping the memory when the OOM error occurs, then you could configure the VM to automatically write a heap dump on out of memory by adding -XX:+HeapDumpOnOutOfMemoryError to the VM parameters (works for Sun and HP VMs).

You can analyze the produced heap dump with Memory Analyzer (an open-source tool recently contributed to Eclipse). If you are interested - link for download and more details: http://www.eclipse.org/mat/
krum


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 5:43 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
You can analyze the produced heap dump with Memory Analyzer

very nice, thank you very much!

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 1:55 am 
Newbie

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

I want to add some missing details:
1. I'm using CMT(container managed transactions) therfore ther is no transactions in the code.

2. The sequence of the process is (300 entities in each iteration):
2.1 Load the entities (relevant code attached above)
2.2 Do some business
2.3 Update the entities

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  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.