File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Question" Watch "Garbage Collection Question" New topic
Author

Garbage Collection Question

Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
Following is a question from Whizlabs simulator:

In the following code, which is the earliest statement, where the object originally held in m, may be garbage collected?

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}


The answer given by Whizlabs is Line 1. But is Line 2 a better answer as you cannot say what happens by calling m.myMehtod(). Suppose this method passes the reference of the current object, say someOtherMehtod(this); , what effect would making m=null in terms of garbage collection? Can you say the object is eligible for garbage collection as the reference passed by someOtherMethod(this); is possibly active? Please clarify. Thanks!
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
You have very good point.

class SomeOtherObject {
private static List<MyObject> lst = new ArrayList<MyObject>();

public static void someOtherMethod(MyObject o) {
lst.add(o);
}
}

class MyObject {
void myMethod() {
SomeOtherObject.someOtherMethod(this);
}
...
}

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}

I will say after Line2 in this case.
[ May 14, 2006: Message edited by: wise owen ]
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi ,

I had the same doubt when I did the whizlabs practice test but I think there is one very important point we must note here:

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}

any method that is called from main method is guaranteed to complete before main is completed. Therefore any references created in those methods will be eligible for GC much more earlier than the ref.s in main except if pass that ref. back to the calling method and store it in some variable. In this question we are not getting any ref.s back from the method "myMethod", therefore, we should be sure of the thing that even if any ref. of "this" was passed to some other method it would have been garbage collected till now and there is no harm in GCing this object after line 1 as soon as the ref. is set to null.

Hope this helps
Asha


SCJP 1.5
Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
...
we should be sure of the thing that even if any ref. of "this" was passed to some other method it would have been garbage collected till now and there is no harm in GCing this object after line 1 as soon as the ref. is set to null.


I am sorry but I am very much confused now. Two replies with contradictory statements. Could anybody please give a final verdict?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Asha:

In the following code, there is not object created. I just simple pass "this" to a static function and which will save the pass in object's reference to a static array.

How can GC collect it before Line 2?


class SomeOtherObject {
private static List<MyObject> lst = new ArrayList<MyObject>();

public static void someOtherMethod(MyObject o) {
lst.add(o);
}
}

class MyObject {
void myMethod() {
SomeOtherObject.someOtherMethod(this);
}
...
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage Collection Question