Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GabageCollection

 
Ivanova Ruslana
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, people!

Can anybody answer the question?!!
Can strings in the pool be eligible for gabage collection?

To answer that question try to answer the following one:
How many objects will be eligible for gabage collection after line //4?

String s1="string1"; //1
String s2=new String("string2");//2
String s3=s2.intern(); //3
s1=s2=s3=null;//4
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they can.
When the class loader that loaded the class containing the String literals is collected, the instances that were created at class load time are also (potentially) collected.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is only one object eligible for GC after line 4.

As a reminder, the special issues involving the String Literal Pool will not be on the SCJP.

But it is a very interesting topic.
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike,
is the object originally referenced by s1 available for garbage collection? I didnt see the correct answer in your so was curious...
thx
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the object originally referenced by s1 available for garbage collection?


According to me, s1 is not available for garbage collection. Only string s2 is availabe. Its because s1 is being referred from the pool.
I have a code which i would like to discuss with u all:


This code gives varied output as u know everytime u run, but i have observed that output generated at line 1 is same as line 3.
But this is not the case with line 2 and line 4.
Does this gives us an idea that still "Ranch" is still being referred? and if thats the case then it means that "Ranch" is being referred from the pool.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i confused with the heap and pool of memory.

can i say that all the objects(using the new operator) will be created in the heap of memory and elegeble for the garbage collection.

the string objects created by mere assignment(String s="string") will be created in the pool of the memory which is not eligible for the garbage collection.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go thru this link for more idea on Strings.
String pool
 
Prabhaavathi Sivagnanam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, this link gives a clear idea abt the how the string literals actually gets stored and when it gets available for garbage collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic