File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GabageCollection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GabageCollection " Watch "GabageCollection " New topic


Ivanova Ruslana

Joined: Mar 20, 2005
Posts: 1
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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.

Tony Morris
Java Q&A (FAQ, Trivia)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.

Mike Gershman
SCJP 1.4, SCWCD in process
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
is the object originally referenced by s1 available for garbage collection? I didnt see the correct answer in your so was curious...

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Jul 19, 2004
Posts: 298
Please go thru this link for more idea on Strings.
String pool
Prabhaavathi Sivagnanam

Joined: Mar 21, 2005
Posts: 13
Yeah, this link gives a clear idea abt the how the string literals actually gets stored and when it gets available for garbage collection.

Thanks,<br />Prabha
I agree. Here's the link:
subject: GabageCollection
It's not a secret anymore!