• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Passed, but 0% on gc

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I was born with webbed fish toes. This tiny ad is my only friend:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic