Results 1 to 11 of 11
  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Garbage Collection - Self reference, static and non-static.

    Hello folks,

    How does Java handle garbage collection for self referencing (non static) classes? And how does it do the same for static self referencing classes.

    Here is what i mean to say, with exmaples :

    Case 1 : non static self referencing class :

    Java Code:
    public class Test1 {
    	public Test1 instance; // non static self reference
    	private static int count; // tough static, it's not a self reference
    	public Test1() {
    		if (count==0) {
    			this.instance = new Test1();
    		}
    	}
    }
    Case 2 : static self referencing class (like singleton) :

    Java Code:
    public class Test2 {
    	private static Test2 instance; // static self reference
    	private Test2() {
    	}
    	
    	public Test2 getInstance(){
    		if (instance == null) {
    			instance = new Test2();
    		}
    		return instance;
    	}
    }
    Three questions here :

    a) in both the cases, there is a self reference, why should the parent object be removed if it has a live reference to it's own kind ?

    b) in Case 2, since there is a static reference, so even the class loader should also have a reference of this object. Should'nt this make garbage collection impossible for such singletons?

    c) In general, what is the criteria in the above two cases , for the garbage collector to pick up objects of these respective classes ?.
    Last edited by garyiskidding; 03-17-2011 at 10:05 AM.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    In the any way, You can't force to invoke a GC and to clean some memory. That is all.
    Java does not provide memory management at all!
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Yea, but my questions are more on the strategy of the Garbage Collection mechanism, not on it's invocation.

    It's a done deal that we can't force the GC to run, but i want to know that when it runs, how does the Java GC implementation look at these cases i've mentioned above, described in the three questions.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i don't understand what you want to know. but to understand the gc you must distinquish between reference and object. the rule is: if there is no reference to an object the object can be garbage collected. now, if a field is static this field belongs to the class and not to the object. so even if the object is nulled, the reference still exists and the object is not garbage collected. look at the following example:

    Java Code:
    class Obj {
    	static Object o;
    	String name;
    	
    	public Obj(String n) {
    		this.name = n;
    	}
    	
    	public void finalize() {
    		System.out.println(this.name + " was finalized");
    	}
    }
    
    public class GcTest {
    	public static void main(String[] args) {
    		
    		Runtime rt = Runtime.getRuntime();
    		
    		Obj o1 = new Obj("o1");
    		Obj o2 = new Obj("o2");
    		o2.o = o2;
    
    		o2 = null;		
    		rt.gc();		
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException ex) {
    			ex.printStackTrace();
    		}
    	}
    }

    inside the class obj the field o is static, so setting the object to null (o2 = null) will not mean that o2 will be garbage collected, since the static reference o still exists. try to remove the reserved word static from o and o2 will be garbage collected.
    Last edited by j2me64; 03-17-2011 at 11:24 AM.

  5. #5
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Rightly said,

    But let me try to explain.

    In the cases above :

    Case 1 : It has a reference called 'instance' to the Test1 object, which is again inside the same Test1 class. If the 'instance' reference to the Test1 object is within the Test1 class, then no-one would ever be able to null the 'instance' reference(tough we can have a method within the Test1 class only to null the 'instance' reference , but not in this case). Given this, can i assume that the 'instance' reference would never be null, so the object will never be garbage collected? So, in general, are objects created through self references never garbage collected?

    Case 2 : Same as above, but since a static class reference is registered with the class loader, this should have even lesser changes of being GC-ied. Can i conclude that singletons will never be garbage collected once they are instantiated?
    Last edited by garyiskidding; 03-17-2011 at 11:21 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,764
    Blog Entries
    7
    Rep Power
    21

    Default

    The garbage collector collects everything that can not (indirectly) be reached through the 'roots'. Classes are roots because they are not unloadable; if they were, singletons couldn't exist (they would be garbage collected). The data stack also is a root, saving the temporary objects and local objects from being garbage collection while they are in scope.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by garyiskidding View Post
    Rightly said,

    But let me try to explain.

    In the cases above :

    Case 1 : It has a reference called 'instance' to the Test1 object, which is again inside the same Test1 class. If the 'instance' reference to the Test1 object is within the Test1 class, then no-one would ever be able to null the 'instance' reference(tough we can have a method within the Test1 class only to null the 'instance' reference , but not in this case). Given this, can i assume that the 'instance' reference would never be null, so the object will never be garbage collected? So, in general, are objects created through self references never garbage collected?
    no. it's right that the 'instance' reference can not be null. but if if you set the object Test1 to null then the object can be gc because also the 'instance' reference comes from a null object.


    Quote Originally Posted by garyiskidding View Post
    Case 2 : Same as above, but since a static class reference is registered with the class loader, this should have even lesser changes of being GC-ied. Can i conclude that singletons will never be garbage collected once they are instantiated?
    yes.

  8. #8
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Yes, and in my case, is'nt the 'instance' reference reachable through the root (the Test1, Test2 classes) ? So should this mean that it would never be garbage collected, the answer to my question?

    Classes are roots because they are not unloadable; if they were, singletons couldn't exist (they would be garbage collected)
    So singletons would never be Garbage collected?

  9. #9
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Okie, got it.

    Thanks j2me64, JosAH

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,764
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by garyiskidding View Post
    Yes, and in my case, is'nt the 'instance' reference reachable through the root (the Test1, Test2 classes) ? So should this mean that it would never be garbage collected, the answer to my question?

    So singletons would never be Garbage collected?
    The reference in class Test1 isn't static so no matter how many times an object refers to itself, if it can't be reached (indirectly) through one of the roots, the object will be garbage collected. The reference in class Test2 is static and just because classes are not unloaded, the classes are roots and the references (the singleton(s)) will not be garbage collected.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Replies: 10
    Last Post: 11-14-2010, 08:06 AM
  2. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 09:17 AM
  3. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  4. Replies: 3
    Last Post: 08-17-2009, 11:59 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •