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

garbage collection

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
somebody please help me out with this:
String x= "Red";
String y="green";
x="hello";
x=y;
x=null;
now how many objects are eligible for garbage collection??
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 objects are eligible for g.c. 'Red' and 'hello'. remember 'x' is a object reference and not an object.
[This message has been edited by Anbooo Sanygao (edited October 26, 2000).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I agree with Anbooo. 2 Objects are ready to be GC.
String x= "Red";
String y="green";
x="hello";
x=y;
x=null;
Initially var x hold the address of object "Red" and y holds the address of object "green".
Then another object "hello" is created and added to the pool and its address is placed in var x. So, now the var x holds the address of object "hello" and not "Red".
"Red" is dangling
After this a copy of address that y holds is placed in x. So, now x has the address of the object "green".
"hello" is dangling
now x which points to object "green" at this point is set to null.
"green" does not dangle because x was only holding a copy of the address and not the actual address.
Actual address is with y and it still points to object "green".
So, Finally we are left with 2 dangling objects which can be garbage collected.

Please correct me if I am wrong
Regards,
Preethi :-))
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String x and String y in this example are present in the String pool.Hence they will not be eligible for GC even if their reference is made null.
If they were created using the keyword "new" then they would be eligible for GC after their reference is made null.
Hence as per my understanding no. of objects eligible for gc in this example should be 0.
Please correct me if it is not so.
 
Author
Posts: 278
11
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, good answer! Yes, Ragini is exactly right. Literal strings (and other compile time constants) are placed in a pool and are not eligible for collection in the usual way.
However, the exam will not ask you nasty tricky questions like that, so you can relax slightly ;>
Cheers,
Simon

Originally posted by Ragini Kelkar:
The String x and String y in this example are present in the String pool.Hence they will not be eligible for GC even if their reference is made null.
If they were created using the keyword "new" then they would be eligible for GC after their reference is made null.
Hence as per my understanding no. of objects eligible for gc in this example should be 0.
Please correct me if it is not so.


 
Ragini Kelkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Simon Roberts.This question was bothering me for quite some time.
reply
    Bookmark Topic Watch Topic
  • New Topic