Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Garbage Collection.

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Object and String literal both are eligible for the Garbage Collection or not which one is eligible?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Literals are not eligible for GC.

find out some smal example

class GC{
public static void main(String args[]) {
String str1 = "abc";
String str2 = new String("abc");

str1=str2=null;
}
}

Only one Object is eligible for GC.
Reason:
In the above code,
1. String1 is the String literal. (this is also object )
2.String2 is the Object.

3. When ever you create a String by using
String str1 = "abc";
Str1 is going to store in Heap Memory.
Str1 is referencing to the String Constant pool.

4. String str2 = new String("abc") is not referencing to the String constant pool.

5. so, when ever we make that strings as null, then
str1=str2=null;

6. Both the string will be null.
7. String2 is eligible to GC, because String1 is still has a reference in String constant pool
8. So, what ever may be the string which has a reference to String constant pool, is
not eligible for GC.

" finally in one word,
String literals are not eligible for GC. "
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic