Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: StatefulPersistenceContext taking lot of memory
PostPosted: Fri Sep 09, 2016 1:39 pm 
Newbie

Joined: Fri Sep 09, 2016 1:29 pm
Posts: 2
when analysing heap dump with eclipse MAT - it shows
org.hibernate.engine.internal.StatefulPersistenceContext @ 0x7162d8da8 shows shallow heap 104 and retained heap 1404448424
seems to be biggest memory is consumed here
our way of calling hibernte is

Code:
   return (Collection) getHibernateTemplate().execute(new HibernateCallback()
      {
         public Object doInHibernate(Session session) throws HibernateException
         {
            return getResultFromQuery(session, queryStr, fetchSize, nextRecord, parameterNames, parameters);
         }
      });



private Object getResultFromQuery(Session session,
                                     String queryStr,
                                     int fetchSize,
                                     int nextRecord,
                                     String[] parameterNames, Object[] parameters)
   {
      Query query = session.createQuery(queryStr);
      if (fetchSize != -1)
      {
         query.setFirstResult(nextRecord);
         query.setMaxResults(fetchSize);
      }
      for (int i = 0; i < parameterNames.length; i++)
      {
         String name = parameterNames[i];
         query.setParameter(name, parameters[i]);
      }
      List list = query.list();
      if (notSupportDistinct)
      {
         return new LinkedHashSet(list);
      }

      return list;
   }


the out of memory stacktrace is pointing to where we get list value

Code:
      List list = query.list();


what are we doing wrong ?
how can memory usage be reduced ?


Top
 Profile  
 
 Post subject: Re: StatefulPersistenceContext taking lot of memory
PostPosted: Sat Sep 10, 2016 11:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1433
You're probably selecting way too many records, so there are two things you could do:

1. You could restrict the ResultSet using pagination, as you might already be using. It does not make sense to retrieve more records than you can display in a UI.
2. If you don't plan on modifying these entries, you could just fetch entries in read-only mode:

Code:
Query query = session.createQuery(queryStr);
query.setReadOnly( true );

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: StatefulPersistenceContext taking lot of memory
PostPosted: Mon Sep 12, 2016 4:00 pm 
Newbie

Joined: Fri Sep 09, 2016 1:29 pm
Posts: 2
thanks a lot for your suggestion . One followup question I had was how setting to readonly mode helps?


Top
 Profile  
 
 Post subject: Re: StatefulPersistenceContext taking lot of memory
PostPosted: Tue Sep 13, 2016 4:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1433
In read-only mode, Hibernate will not retain the dehydrated state which it normally uses for the dirty checking mechanism. So, you get half the memory footprint.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.