Results 1 to 11 of 11
- 03-17-2011, 09:01 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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 :
Case 2 : static self referencing class (like singleton) :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(); } } }
Three questions here :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; } }
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 09:05 AM.
- 03-17-2011, 09:38 AM #2
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
- 03-17-2011, 09:43 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 03-17-2011, 10:07 AM #4
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 10:24 AM.
- 03-17-2011, 10:18 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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 10:21 AM.
- 03-17-2011, 10:43 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-17-2011, 10:49 AM #7
- 03-17-2011, 10:51 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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?Classes are roots because they are not unloadable; if they were, singletons couldn't exist (they would be garbage collected)
- 03-17-2011, 10:52 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Okie, got it.
Thanks j2me64, JosAH
- 03-17-2011, 11:13 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-17-2011, 11:22 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Cannot make a static reference to the non-static method
By Xycose in forum New To JavaReplies: 10Last Post: 11-14-2010, 07:06 AM -
static variable /pass by reference
By katturv in forum New To JavaReplies: 15Last Post: 10-03-2010, 08:17 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
Static and non staic reference, Synchronized and unsynchronized, abstarct class,
By Basit56 in forum New To JavaReplies: 3Last Post: 08-17-2009, 10:59 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks