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 String objects and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String objects and garbage collection" Watch "String objects and garbage collection" New topic

String objects and garbage collection

Allan Morris

Joined: Sep 03, 2006
Posts: 4
Hi, there's a practice paper :

with the question:

36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
where the answer is a multiple choice of 0,1,2,3 or 4

I answered 0 while the answer sheet is saying 1, i'm not sure I understand the reason.

This was my logic:

String name; //1 variable, 0 objects
String newName = "Nick"; //2 variables, 1 object(value is obtained from string pool)
newName = "Jason"; //same object now references the "Jason" literal
name = "Frieda"; //2 variables, 2 objects

String newestName = name; //3 variables, 2 objects

name = null; //3 variables(one of which is null), still 2 objects.

So I ended up with 0 objects eligible for garbage collection, where am I going wrong with this please?
[ September 05, 2006: Message edited by: Allan Morris ]
Shang Lee

Joined: Aug 17, 2006
Posts: 20
Hi Allan,

newName refers to "Nick" but in the next line, it refers to "Jason". "Nick" is now eligible for garbage collection.

Best Wishes,<br />Shang
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Allan,

You are quite correct !!!

With no fear to miss this question on the exam. There are NO objects eligible for GC.

Any hardcoded strings created in a Java application are stored on String pool.

For those objects be eligible for gc they must be created with new.

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
parshuram bingi

Joined: Aug 01, 2006
Posts: 10
Hi all,

I don't know exactly but, I guess when the object in the pool is not been pointed by any object it should be eligilble for the Garbage collection right. If not at one time or the other, the pool will bulge and the porcessing time will grow. am I right?


Hard Work, Honesty(2H) pays...
fred Joly
Ranch Hand

Joined: Jan 19, 2006
Posts: 55
Just found this in this very site.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Garbage collection always takes place on the heap. Since none of the String instances above are on the heap, the no. of objects garbage collected is 0.
[ September 06, 2006: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
SCJP page 413-415 reads:

String s = "abcdef";

is a shortcut for

String s = new String("abcdef");

Then they go on to show that the 1st example creates "abcdef" on the heap. figure 6.1

So it seems to me, by the K&B definition, that the answer to the original question is: 1 object is eligible for garbage collection.
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
I just re-read the link that Fred posted.

It appears that objects in the string pool are eligible for garbage collection, but not until the program is unloaded. So, for the exam, it seems to me it makes sense to think of all strings as objects that are eligible for GC if nothing references them. The specifics of the pool are not important for the exam.
I agree. Here's the link:
subject: String objects and garbage collection
It's not a secret anymore!