-->
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: Memory problem with parent-child tree structure and cache
PostPosted: Mon Nov 27, 2006 9:29 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
Hello Hibernate Users

I'm still using Hibernate 2.1.8 but I would appreciate replies telling me if my problem is something Hibernate 3 solves.

I have a tree data structure with 1 to many mappings from parents to children where children can also be parents etc. The hbm file is as following:

Code:
<hibernate-mapping>
  <class name="TreeNode" table="tree_node">
    <cache usage="nonstrict-read-write"/>

    <id name="id">
      <generator class="assigned"/>
    </id>

    <property name="name" length="255" not-null="true"/>
    <!--
      Lots of properties and other relationships omitted for clarity...
    -->

    <many-to-one name="parent" column="parent_id" not-null="false"/>

    <list name="children" cascade="none">
      <cache usage="nonstrict-read-write"/>
      <key column="parent_id" />
      <index type="integer">
        <column name="page_index" not-null="false"/>
      </index>
      <one-to-many class="TreeNode"/>
    </list>
  </class>
</hibernate-mapping>


This solution seemed to do the job quite nicely. The caching makes it so that when recursively traversing the tree and rendering the contents database access is minimal. This data is not frequently changed it seems pretty much the whole tree structure is stored in the cache (this is fine).

The problem comes when multiple users start accessing the data. What seems to happen is that for each Session, Hibernate returns separate instances of all the classes so that for each user requesting the tree or part of it there is a whole other instance of the complete tree in memory. For large tree's this runs out of memory on JVM's with a gigabyte of max memory. This is at least what I think is going on, the profiler tells me 90% of the memory is used for TreeNode objects.

Making the list of children lazy and removing the cache solves the memory issue, but will result in N sql queries for traversing a N node tree, which is way to slow.

Is there a way I can set this up so that I get the speed benefit of using the cache but not eat up all the memory?

Cheers!
Sindri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 9:42 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
try setting batch-size on the one-to-many set.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:06 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
gopalsaha wrote:
try setting batch-size on the one-to-many set.

Setting batch-size to ~10 improves the speed of the lazy fetching by a lot. However it is nowhere near the performance I got when I had the collection cached.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:41 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
right but are you not using secondary caching.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 11:03 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
gopalsaha wrote:
right but are you not using secondary caching.

I turned off the second level cache for the collection and that seemed to solve the memory problem I was having. It's possible it was the lazy="true" that actually solved that.

I've had problems with using both lazy="true" and cache on collections (lazy collection occasionally being accessed after the session is closed). Maybe I should be focusing on solving that and use both lazy="true" and 2nd level cache.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 11:53 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
look into opensessionviewfilter
hope this helps and if so please do rate


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.