This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Certification Results and the fly likes Passed, but 0% on gc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Certification Results
Bookmark "Passed, but 0% on gc" Watch "Passed, but 0% on gc" New topic
Author

Passed, but 0% on gc

Shiao, Chern-Jer
Greenhorn

Joined: Aug 18, 2001
Posts: 7
Just passed today, but got 0% on Garbage Collection. I found that is't difficult to study gc behavior because I can't do a simple programming test.
String o=new String("true");
String[] oa=new String[1];
oa[0]=o;
o=null;
oa[0]="";
oa=null;
return;
How do I know when o is eligible to gc?
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
I got 0 questions on GC also.
Below:
1. String o=new String("true");
2. String[] oa=new String[1];
3. oa[0]=o;
4. o=null;
5. oa[0]="";
6. oa=null;
7. return;
Line 4 is where o is eligible for gc. It looks as if this is in a method in which case the local variables will be eligible after the method ends.

Sun Certified Programmer on the Java 2 Platform
Matt Schwarz
Greenhorn

Joined: Aug 16, 2001
Posts: 12
I might be wrong, but I disagree with Tom. First off, Garbage Collection questions are usually asked "When is the OBJECT created at line X eligible for GC?" "o" is a reference to a string, not an object. The object is the String with the text "true".
An object is eligible for garbage collection when there are no active references to that object. In line 3, a second active reference is created when the first reference in the array oa is pointed at the string. Therefore, when o is set to null in line 4, there is still an active reference.
When oa[0] is set to "" in line 5, there are no longer any references to the string containing "true", so it is then eligible for GC, after line 5.
Matt
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
Good point there. I could have sworn I got the questions right on the Test Exams. I thought it was able for GC once it changed the value to its variable. The variable to "true" in line one is one value but once it got assigned null (on line 4) it was then a new value then the o on line one is then eligible for GC and a new value for o is made on line 4.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passed, but 0% on gc
 
Similar Threads
Garbage collection Question
when is eligible for garbage collection?
GC help needed