• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage collection

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code, at what line is the object anObj available for garbage collection?

The answer is line 10, why?
Here is another one: at what point is the object anObj available for gc:

The answer is line 11.
I think an object is eligible to GC as soon as it is assigned a null value, but both answers are not pointing to the line where it is assigned null value. Please explain it, thanks in advance.
Luk
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hello Dear
The things are like this in your first example, object is garbage collected when the reference pointing to the object cannot get them.
first off all the strings are immutable.
in this case anObj.trim() you are not storing the result.so nouthing will happens.
but when you say anObj=anObj.toUpperCase() here after this anObj is pointing towards some different & not on "sample",so you cannot get back your object "sample", after this line its lost.so it is garbage collected.
In second question locObj is a local variable its scope is limited to its block only.
here "sample" obj. is refered by two varible namingly anObj & locObj, so though anObj is become null "sample" is still refered by the locObj & is not lost."sample" lost only after the scope of the block ends means after line no 11.
Love
Lokesh
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear fellow,
u must differtiate between a variable or variable name and an object that occupying space. GC one and only key rule is
"An object(occuping space) gets eligble for garbage collection when it could not be accessed again or any refrence to it has be lost or destroyed"
in your both question 'anObj' is variable name not the object where as "sample" is the real obj so untill u have a refrence to it, it can not be garbage collected
07: anObj.trim(); as "sample" have nothing to trim so no new obj get created nor u r dislocating the anObj so an obj is still pointing the smae "sample"
09:anObj=anObj.toUpperCase();
as "sample" and "SAMPLE" is different objects which u can confirm by insering this line of code in your code
String anotherObj=anObj.toUpperCase();

if (anotherObj==anObj){
System.out.println("equal");
}
else{
System.out.println("unequal");
}
'unequal' get printed it means that new obj get created and its refrence has been assign to the anotherObj.
where as in your case 'anObj' is get assigned new obj refrence. so the old 'sample' is ready for garbage collection as it does not have any var to get refrenced in program.
in your next question the explanation is same what only matters is the local scope of 'anObj' and 'locObj' var after that 'sample' could not be refrenced so it is eligible for garbage collection just after 'if'
hope u not get confused
Nayyer

 
Lokesh Mahajan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Nayyer
in ref. to your statement.
anObj.trim(); as "sample" have nothing to trim so no new obj get created nor u r dislocating the anObj so an obj is still pointing the smae "sample"
---------------------------------------------------------------
you are right that if there are no spaces as in the case of "sample" anObj.trim() will not create new obj. but this is only true if your newly created obj. is refered by some varible.
ie anObj = anObj.trim().
public class Hi
{
public static void main(String args[])
{
String s1 = " sample ";
s1.trim();
System.out.println(s1);
}
}
here in this example it is not going to trim the spaces of the s1
because you are not storing the newly created obj. after triming,
but in the following example
public class Hi
{
public static void main(String args[])
{
String s1 = " sample ";
s1 = s1.trim();
System.out.println(s1);
}
}
here in this obj. created after triming is store back in the s1, so here if you print the s1,you will find that the spaces are trimed,
So though " sample" is used insted of just "sample" it would have no effect, because new obj created is not refered by any variable just s1.trim() does not store the obj. back in s1.
Love
Lokesh

 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear folks,
Thanks so much for your terrific input. Please confirm this:
Whenever I got String objects involved in GC questions, I should be careful 'coz Stirng is immutable and (most) String's methods will create a new String if they did make some changes of the original object of String, and if no change is made, the new String actually is still the old one because no new object is created by the method at all.
Thanks.
Luk
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic