• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question about Garbage Collection

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across following question.

At what point will the object created on line 8 be eligible for garbage collection?

1.public class RJMould{
2. StringBuffer sb;
3. public static void main(String argv[]){
4. RJMould rjm = new RJMould();
5. rjm.kansas();
6. }
7. public void kansas(){
8. sb = new StringBuffer("Manchester");
9. StringBuffer sb2 = sb;
10. StringBuffer sb3 = new StringBuffer("Chester");
11. sb=sb3;
12. sb3=null;
13. sb2=null;
14.
15. }
16.}


The given answer is Line 13.

My doubt:

Object sb is assigned value at line 8.
sb is again assigned some value at line 11.

Line 12 makes sb3 null and line 13 makes sb2 null . But still sb holds some value.
So how could sb is eligible for garbage collection at line 13? Isn't it hold some reference(which point to object creted with sb3 ref)?

According to me it should be eligible for GC after execution pointer is out of main() method ie. after programe is ended.

Please expalin me if I am missing some concepts.

Thanks in advance,

Vivian
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
The given answer is correct it becomes eligible at line 13.
According to GC the object are eligible for garbage collection when it is not accessible by live thread.

8. sb = new StringBuffer("Manchester");


Now here an object is created in the heap

sb --->> "Manchester"

sb is holding a reference of manchester.
Manchester is an object in the heap not sb .


Now
9. StringBuffer sb2 = sb;
after this line


sb--->> "Manchester"
sb2-->> "Manchester"

---------------
10. StringBuffer sb3 = new StringBuffer("Chester");

now after this line
sb-->> "Manchester"
sb2-->> "Manchester"
sb3-->> "Chester"
-----------------

11. sb=sb3;
Now after this line



sb-->> "Chester"
sb2-->>"Manchester"
sb3-->>"Chester"

after line 12
that is
12. sb3=null;
---------
sb-->> "Chester"
sb2-->>"Manchester"
sb3-->> NULL

after line 13
this is what happens

13. sb2=null;
sb-->> "Chester"
sb2-->> NULL

sb3-->> NULL



so the reference variable the only reference variable holding the reference of manchester is set to null and hence the object created at line 8 (Manchester) is eligible for garbage collection


Hopefully you got it ..!!

And they have asked the Object eligible for GC not the reference variable ..this is were you are losing it
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object created in line 8 = new StringBuffer("Manchester")

After line 13 there is no reference to this object. So this object is eligible for garbage collection

sb is refering to StringBuffer("Chester") and not to the Stringbuffer("Manchester");

The question means the object created and not the reference variabele!!
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a difference between the OBJECT created on line 8 (a StringBuffer with the value "Manchester") and the REFERENCE declared on line 8 ("sb").

Make no mistake - they are two separate things. the object/StringBuffer is like your house. the reference ("sb") is a piece of paper with your address on it.

the line "StringBuffer sb2 = sb;" makes a copy of what's on the paper - you now have TWO pieces of paper with the same address on them, but still one house.

Line 10 creates a new object (your summer home, perhaps?), and a new, third reference/piece of paper with THAT home's address.

on line 11, you change the address written on the sb piece of paper to have your summer home. Now sb and sb3 both refer to your summer home, and sb2 to your other home.

sb3 = null erases the address off that paper. so sb points to the summer home, sb2 to your normal home, and sb3 is blank.

sb2 = null erases what's on that paper. you now have NO paper with your normal home address on it. since you don't have your home address anymore, you can't get there since you can't find it. therefore, someone (the GC) comes along and tears down your home and builds a home for someone else.

note that the sb REFERENCE still points to A home, but not the one created on line 8.
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for such a quick reply.

I got the point. I was confusing between REFERENCE and OBJECT. Everyone's detailed explanation cleared my doubt. Thanks again for the efforts.

- Vivian
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic