• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passed, but 0% on gc

 
Shiao Bing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Schwarz
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic