• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

GC - String object

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why my String object created is NOT eligible for GC ?


Thanks for the reply
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you're studying the GC for the exam, use any kind of object other than a String object - the String constant pool makes the interaction between Strings and the GC weird, and it's not on the exam. But for sure, non-String objects and the GC are on the exam big time!
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Wondered How Hell this Code Gets Compiled
class testing{
protected void finalize(){
System.out.println("Object GCd");
}
void nothing(){
String s2 = new String("Susan");
s1=null;
System.gc();
}
public static void main(String args[]){
testing t = new testing();t.nothing();
}
}

What is s1???
 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Susan, try this code and you will get idea of any class other than String

class A{
protected void finalize()
{
System.out.println("Object A GCd");
}
}
public class testing{
protected void finalize()
{
System.out.println("Object GCd");
}
void nothing()
{
String s2 = new String("Susan");
s2=null;
System.gc();
}
void tryThisMethod()
{
A a1 = new A();
A a2=null;
a2=a1;
a1=null;
a2=null; // after this statement execution, the object of A is eligible for GC
System.gc();
}
public static void main(String args[]){
testing t = new testing();
t.nothing();
t.tryThisMethod();
}
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rushikesh Wagh:
I Wondered How Hell this Code Gets Compiled
class testing{
protected void finalize(){
System.out.println("Object GCd");
}
void nothing(){
String s2 = new String("Susan");
s1=null;
System.gc();
}
public static void main(String args[]){
testing t = new testing();t.nothing();
}
}

What is s1???




I guess that was the typo error. read it as s2 = null
reply
    Bookmark Topic Watch Topic
  • New Topic