Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent......Garbage Collection

 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can String literals ever be garbage Collected?

eg
String s1="abc";
String s2="aaa";
s1=null;
s2=null;
When will be s1,s2 be garbage collected?
Why?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are created on the heap they can get gc'd, if they are created in the Constant Pool they will not.
If they are resolved at compile time (like your examples) they will (probably) get created in the Constant Pool.
If they are created at runtime like
String s3 = s1+s2;
they will (probably) be created on the heap.
The reason for the disclaimer is that Sun does not really tell vendors how to implement the specifications. Some PHD student might create a very inefficient JVM that implements EVERYTHING on the heap (not a very good idea).
 
Pratap Reddy
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shashank,
There is no way to find gc'd. Since they are stored on heap. Only objects which created on heap are gc'd. Or else all objects created with new keywords are gc'D.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic