• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Garbage Collection Doubt

 
Srinivas Kollaparthi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code at the end, how many objects are eligible for Garbage Collection.

String str=new String("hello");
str = null;

I believe the answer is 2.
Is my answer correct ?
Why I am confused is the question asks for objects eligible for GC.
Is String literal "hello" also an Object ?
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Answer to the given problem should be 1. Here actually two objects are created ,one is in String pool and one other but both will be in the heap.Only object other than String pool is eligible for garbage collected.Objects in String pool remains in the heap.They are not collected by G.C.

Please post your query in SCJP forum ,Not in this forum.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true as pradeep singh told.

You may read this article to get to know more about the objects referred to by the references in string pool are NOT eligible for GC!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic