• 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 question

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
--------------------------
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hai ,

1) Initially name is pointing to null.
2) newName is pointing to "Nick" ,( Object creted in String Pool)
3) newName is pointing to "Jason" , ( -do-)
so nothing is pointing(referring) to "NICK"
4) now name is pointing to "Frieda" (-do-)
5) newestName is also poining to "Frieda"
6) name is null ,but newestName is pointing to "Frieda"
7) So , only "NICK" can be gc'ed.
regards
Sateesh
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sateesh,
Thank You very much for your clear explanation. I also had a doubt about this kind of questions.Thank a lot for helping me to clear my problem.

Shan.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the above piece of code the String literal "Nick" would not be garbage collected since this would go to the String pool.
Can somebody throw some light on Garbage collection with respect to String pools ?
Thanks
Subbu
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The JVM Maintains a String Pool... for example...
1: String s1="Hello";
2: String s2="Hello";
3: String s3=new String("Hello");

In Line 1, the JVM creates a String "Hello" (LITERAL), and passes the reference to s1. In Line 2, the JVM sees that another string "Hello" has to be created, instead of creating one, it passes the reference of the previously created String to s2. FOR EFFICIENCY. That's why (s1==s2) is true.
The JVM maintains this pool.. consisting of strings created from only LITERALS. Hope you got the point. So you can see that (s2==s3) returns false.
ANYBODY: is this correct?
Bye
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You are right nutan. JVM maintain string pool of literals.
In above example all are literals and no objects.
As objectes are created with new operator, which is not done here.
So will these literals will be garbage collected
Pleaethrow some light on which one will be garbage collected
Parag
 
Subramaniam Chidambaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the String literals in the string pool will not be garbage collected.
But m doubt is will it be garbage collected only when the JVM is restarted ?
Thanks
Subbu
 
reply
    Bookmark Topic Watch Topic
  • New Topic