• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Urgent......Garbage Collection

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic