-->
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.  [ 3 posts ] 
Author Message
 Post subject: Out of memory while fetching data in query.list()
PostPosted: Mon Nov 17, 2008 8:01 am 
Newbie

Joined: Mon Nov 10, 2008 3:46 am
Posts: 12
Hibernate version:3.0

Name and version of the database you are using: HSQL in server mode

I am having a very large input data file of about 100,000 records and I need to operate for each record. I first tried the easiest method to call query.list() but at this line I was getting out of memory. So I changed my code to use pagination for fetching, initially it seemed to work fine but after completion of some runs it again gave me out of memory. So what I understood from this and using a profiler to see memory usage is that although the memory required did not increase in one go, but it did rose to the same level as using query.list(). My second level cache is off. I just want to know, is there any way to optimize memory usage. I have already allocated 256M of memory and can not afford to give more.


Code:
   private boolean hasNextObject()
   {
      if(_sourceIterator != null && _sourceIterator.hasNext()){
         return true;
      }
      return nextBatch();
   }

   public Object fetchNextRawObject()
          {
      
      if(hasNextObject()){
         return _sourceIterator.next();
      }
      
   }
   private boolean nextBatch()
   {
      ++_fetchNo;
      int dataNumber;
      dataNumber = (_fetchNo - 1) * MAXIMUM_DATA_PER_FETCH;
      
      _query.setFirstResult(dataNumber);
      _sourceIterator = _query.list().iterator();
      return _sourceIterator != null && _sourceIterator.hasNext();
   }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 9:02 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Are you clearing your session (eg. calling Session.clear()) at regular intervals?

You can try to use Query.iterate() or Query.scroll() instead of Query.list(). Query.iterate() combined with regular Session.clear() should consume very little memory at the Hibernate level at the cost of more queries to the database.

Another option (that can be combined with the above) is to use a StatelessSession instead of a regular Session. Since a StatelessSession doesn't have a first-level cache it will use a lot less memory and you don't have to call clear().


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 1:10 pm 
Newbie

Joined: Mon Nov 10, 2008 3:46 am
Posts: 12
Thanks nordborg!! I was calling session.flush() but was not clearing it. Also using iterative instead of list also saved a lot of memory...
I could not try with statelesssesion as some required features of normal session were not present. Thank you for your quick response!!


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