File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection question

 
sarim raza
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sateesh Kumar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Shan Karawita
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Subramaniam Chidambaram
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
nutan prakash
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
parag bharambe
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic