-->
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.  [ 2 posts ] 
Author Message
 Post subject: Memory Leak while searching: HQL-Queries never freed!
PostPosted: Thu Jan 26, 2006 3:13 pm 
Newbie

Joined: Thu Jan 26, 2006 2:13 pm
Posts: 9
Hi all

We're using Hibernate in our business application and it's doing it's job very well. However, when I implemented the search functionality, I'm getting those nasty OutOfMemoryErrors. My approach is a bit special, but I didn't see another solution. Perhaps you might help me find the memory bug or help me improving my search-system with hibernate.

This is the way I did it:

- SearchCriterias are entered.
- Objects on the table are sought.
- Then from the retrieved Objects I extract every ID and store them in a collection. Lazy collections helps me here in speed. (I need data from child-objects but it would be very time-expensive if I would first get the objects and then do calculations with these)
- Afterwards, I go through that list of IDs and create SQL-Queries for a view I created which delivers me exactly the data I need (Objects are far to deeply nested to calculate data with children). Query example: from v_myView where id = 1 and id = 2 etc.

These queries are huge of course. But it works. I split them to portions of 1000 IDs per query in order to avoid overflows.

Now everything's fine, but...

My memory increases with every search and finally the OOME occurs.
I profiled and memory-debugged it and came to the conclusion that there are way too many antlr.CommonAST and org.hibernate.hql.ast.SqlNode, .LiteralNode, .IdentNode objects. They increase with every search almost a 100 percent. First I presumed that the Hibernate Query Cache was on, but we set the property correctly to false in our config file. Query.setCacheable(false) didn't help either.

Has anyone experienced the same problems while searching with too many or too long queries before? Or does anyone know how to optimize our search method using hibernate?

I appreciate all your help. I tried so many things and still can't figure out what's wrong... Quite frustrating, you know.

Somewhere I read, that I should close the session factory to solve this problem (or a similar one), but even with closing, flushing or clearing every session and sessionfactory that is available, it just did not work.


Here is my set up:

Hibernate version: 3.0.5

Code between sessionFactory.openSession() and session.close():

Code:

// <-- ids retrieved and stored in foundIds (ArrayList)

int iterationStep = 1000;
int total = foundIds.size();
HashSet result = new HashSet(total);

// do several smaller queries
for(int count = 0; count < total; count += iterationStep)
{
    // the upper limit of ids to fill in the query
    int upperLimit = count+iterationStep > total ? total : count+iterationStep;
    logger.info("from: "+count+" to (excluding) "+upperLimit);
   
    // generates our current query with a portion of the ids
    String strQuery = generateSearchOrdsQuery(foundIds.subList(count, upperLimit);
   
    // creates the query
    Query query = HibernateUtil.getSession().createQuery(strQuery);
   
    // and executes it
    result.addAll(query.list());
    logger.info("subtotal: "+result.size());
}

// the query generator-method
private String generateSearchOrdsQuery(List foundIds)
{
    StringBuffer hqlQuery = new StringBuffer("from VSearchOrds ");
    // rem: VSearchOrds is the name of the used view
   
    if(foundIds != null)
    {
        hqlQuery.append(" where ");
       
        // now add the ids to this query
        for(Iterator iter = ordsIds.iterator(); iter.hasNext();)
        {
            Integer foundId = (Integer)iter.next();
            hqlQuery.append(" id = " + foundId.toString() + " or ");
        }
        hqlQuery = new StringBuffer(hqlQuery.substring(0, hqlQuery.length()-3)); // to remove the last ''or''
    }
    return hqlQuery.toString();
}



Full stack trace of any exception that occurs: java.lang.OutOfMemoryError (no stack trace available)


Greetings from Switzerland! (I'll send you a postcard, if you can help me solving my problem =)


Top
 Profile  
 
 Post subject: Hi
PostPosted: Mon Jan 30, 2006 12:53 pm 
Newbie

Joined: Mon Jan 30, 2006 11:28 am
Posts: 13
Location: Mumbai India
Hi,
I am very old user of hibernate. We are successfully using hibernate 2.x version in one of our applications.

Now we are using hibernate 3.x (3.1.2).
------- hibernate.cfg.xml -
<property name="dialect">
org.hibernate.dialect.DB2Dialect
</property>

<property name="hibernate.connection.release_mode">
auto
</property>
<property name="hibernate.current_session_context_class"> thread
</property>

1) Using session per request pattern with ThreadLocalSessionContext. Everything works well functionality wise.
2) Session factory is building code is taken from HibernateUtils.java from CaveatEmptor sample application.
3) Session is created like a caveat emptor application with same web filter.

4) Transaction is committed and session is closed from web filter. Which automatically unbinds the session from ThreadLocal.

5) SessionFactory instance is closed and set to null in Application ContextListener.


I have gone through all the postings related the memory leaks and up to date with all the progress. Aware of all the release notes, eg upgrade cglib to fix memory leak etc.


Even now, i m facing memory leak problem. It grows and end up with out of memory on WAS 5.1. Same is the case on jboss 4.0.2. I have removed the hibernate sar from deploy directoy so that the classes are always loaded from my web application.

Please some one help me.

Help me. Help me.

I think i have the similar problem quite close like you. I have the huge ecomm application where thousands of query perfomed. And after each request, memory grows continously.
Typically, i would expect the memory to increase and them come down once request is processed.
I configured the web application HTTP Thread pool with 10 min and 25 max threads. Pool thread never expires. This helped me to make the application run for longer time but not forever.

Which made me to conclude that processor thread are not releasing memory. And memory is leaked when the thread is dead. (Thread instances not garbase collected).

I am using following jars in my WEB-INF/lib folder :
commons-anm-1.0.0.jar
ism-dist.jar
jstl.jar
lucene-1.4.3.jar
myfaces-extensions.jar
myfaces-impl.jar
myfaces-jsf-api.jar
standard.jar
struts.jar
commons-beanutils.jar
commons-codec-1.2.jar
commons-dbcp-1.1.jar
commons-digester.jar
commons-fileupload.jar
commons-httpclient.jar
commons-lang-1.0.1.jar
commons-pool-1.1.jar
commons-validator.jar
antlr-2.7.6rc1.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
concurrent-1.3.2.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
oscache-2.1.jar
hibernate3.jar (hibernate 3.1.2)

I m ready to product any other data if needed.

Regards,
-Chandra

_________________
-Chandra Singh (Anmsoft)


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