• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collection  RSS feed

 
KrishnaRaj Radhakrishnan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we invoke gc explicitly in a mock exam question it is found that set reference to null and reassigning it to another object there is a chance of getting garbage collected codei s like this
cosider this is in main method
1)Strin s1 = "aaa";
2)Strin s2 = "bbb";
3)Object a = s1;
4)a = null;
5)a = b;
Quetion is where is object a likely to get garbage collected line numbers is given as choice
Please clarify
Rk Raj
 
Balaji M
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Strin s1 = "aaa";
2)Strin s2 = "bbb";
3)Object a = s1;
4)a = null;
5)a = b;
Here variable is available for garbage collection just before line no:5 or to put in another way, after the execution of line no:4.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Balaji:
Here variable is available for garbage collection...
a small correction...
What you seem to say is correct, however
the variable is not the one which is being GC. The object
referenced by the variable
is being GC, since it has no
further references.
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here should be small corection it should be String //Strin
U dont b for which it is refering // not clear for b;
String a="skldjf";
String s2 ="adfkl";
Object a=s1;
a=null; //if u say a to null still s1 is referring to that string object
no garbage collection.
i hope it is clear.
suresh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!