This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Urgent......Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Urgent......Garbage Collection" Watch "Urgent......Garbage Collection" New topic
Author

Urgent......Garbage Collection

shashank hiwarkar1
Greenhorn

Joined: Feb 11, 2001
Posts: 25
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?


shashank
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).

"JavaRanch, where the deer and the Certified play" - David O'Meara
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Urgent......Garbage Collection
 
Similar Threads
string literal GC
Garbage Collection
what is the trick
Confusion in Garbage Collection
gc and the string pool