-->
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: Garbage Collection of objects with bidirectional association
PostPosted: Fri Jun 02, 2006 1:49 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
Say I have two objects, Parent and Child, and they both have one-to-one mappings as below,

Code:
<class name="Parent" table="PARENT">
    ...
    <one-to-one name="child" class="Child"/>
</class>

<class name="Child" table="CHILD">
    <id name="id" column="ID">
        <generator class="foreign">
            <param name="property">parent</param>
        </generator>
    </id>
    ...
    <one-to-one name="parent"
        class="Parent"
        constrained="true"/>
</class>


So in the java classes, Parent has a reference to Child and Child has a reference to Parent.

How exactly are these classes garbage collected once they are loaded into memory? Does the session deference both these instances from one another once the session is closed?

Are objects with bidirectional many-to-one associations handled the same way too?


Top
 Profile  
 
 Post subject: Re: Garbage Collection of objects with bidirectional associa
PostPosted: Fri Jun 02, 2006 3:10 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
In Java, objects are garbage-collected when they can no longer be reached from any top-level reference. For example, consider this simple code:

Code:
public void foo() {
  Parent parent = new Parent();
  Child child = new Child();
  parent.setChild(child);
  child.setParent(parent);
  System.out.println("Yey !");
}


When you call foo(), it will create the two objects. When foo() returns, however, both objects will be garbage-collected (*), because there is no way anyone could ever access them from anywhere.

In other words, Java does not use reference-counting for garbage collection; it uses some other algorithm which is nearly magical in its performance speed and accuracy. I honestly have no idea how it works (I'm a n00b), but it does.

Things are not as simple with Hibernate, because Hibernate has a cache that will keep the references to these objects. However, once the objects are evicted from the cache, they'll be gone (assuming that you're not holding a reference to them somewhere).

(*) Actually, they will only be marked for garbage collection; they will only be garbage-collected for real when the JVM decides that it needs more memory.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.