GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string pool literals are not garbase collected ?? 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 "string pool literals are not garbase collected ??" Watch "string pool literals are not garbase collected ??" New topic
Author

string pool literals are not garbase collected ??

Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
Hi ,
I come across this question in JDiscuss.com
Question)How many objects will be eligible for GC just after the method returns?
public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}
For the question I thought the answer is 2 but in the JDiscuss.com they gave answer as 1 with the below explanation.
Explanation:
Objects passed to the method are never garbage collected in that method. So p cannot be GCed.
x is not an object.
"abc" is a string literal which goes to the string pool and is not GCed.
So, only a is eligible for GC.

Here my question is why String object str is not eligible for garbase collection ? . As far as I know all objects will be garbase collected when method returns . Pls explain me why JDiscuss.com says string literal which does goes to the string pool is not GCed.
Regards
Raju


SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When it comes to garbage collection, String are a special case. String literals are immutable and, based on that, some degree of optimization is done. Basically, any String literals that you have within your program are referenced from a "constants" table. Because that reference always exists, a String literal will never be garbage collection.
Here's the key point to keep in mind, though - Garbage Collection of String Literals IS NOT on the SCJP exam. If you really want to know about it, you can do a search over this forum and you'll discover tons of information about it.
Corey


SCJP Tipline, etc.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Let me point out that you never used the keyword "new" for the "str" variable. Therefore, no object was created. All that was done was you created a new reference variable to point to the string in the string pool. Quite literally, the reference variable you know as "str" is a stack variable (aka automatic, local), so it's just going to get popped off the stack. The string pool object of course will continue to exist.
Does that make more sense now?


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
Hi ,
Thanks for your wonderful explanation.Now I am clear that if we define String object without new (ex: String str="hello")then it will not be consider as eligible for garbase collection.
Cheers
Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
I am not sure at all what is happening. I just came across a mock exam question in Simon Roberts/Heller:

The answer is B. ie One object, only line 1 created an object.
Reason:
----------
Line 1 creates a string object. Line 2 creates a second reference to that string. Line 3 appears to create a different object, but because literal strings are taken from the literal pool without duplication, line 3 only creates a third reference to the same object.
How do we reconcile his points to this topic now?
[ March 29, 2004: Message edited by: Frankie Chee ]
[ March 29, 2004: Message edited by: Frankie Chee ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
This one (though poorly worded) is only asking how many objects are created--not how many will be eligible for garbage collection. Yes, there is only one String in the whole example: "abc". But, there won't be any objects eligible for garbage collection. So, the who aren't opposed afterall.
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Ranchers,
Just go thru the below Q.
From the following code how many objects are eligible for garbage Collection?

What is the correct answer?
A/c to the expanation provided above, the answer sould be 0 coz here we r not creating any string objects using using New operator.so, no object will be eligble for garbage collection. Am I correct r missing somewhere?
Pls expalin in detail.
Vineela.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
That's correct.
But as a further explanation, it's not that we use the keyword "new" that would be important. When we do use "new", Objects are created on the heap. They remean uneligible for garbage collection as long as a reference variable holds that information AND is assessible from one of the executing threads. (Hopefully that makes sense).
Basically, in order for something to be eligible for garbage collection, it needs to be on the heap, and therefore needed to at some point be created with the keyword "new". Once no reference variables reference that variable, it is eligible for garbage collection.
So, yes, you are correct (in the previous example). No objects will be eligible for garbage collection. And if we could rewrite your explanation some, we would say: no Objects are eligible for garbage collection because there are no Objects on the heap that are no longer referenced by any reference variables accessible from any of the executing threads in the application.
Okay, that was a bit wordy, but did it help?
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Nathaniel ,
i agree with ur expalnation. but in the exam from where i got the q the answer given is 1. i.e is one object is eligible for grabage collection.
Can u explain how is this possible?
Vineela.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Simple explanation: the mock exam question is wrong.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Nathaniel is absolutely correct. For this example:

The answer should be 0. On the other hand, this question should not be part of an SCJP mock exam as this question would never come up on a real SCJP exam.
Corey
atiqur rahman
Greenhorn

Joined: Aug 30, 2003
Posts: 10
String a = new String("AAA");
How many object is created?
Here we use new so if 2 object is created then is one is eligible for garbage collection?
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Nathaniel & Corey,
Thanks for the expalnation.But, i got the Q in a SCJP mock exam itself.I don remember from which mock exam i got.
Regards
Vineela
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Vineela Devi Jakka:
Hi Nathaniel & Corey,
Thanks for the expalnation.But, i got the Q in a SCJP mock exam itself.I don remember from which mock exam i got.
Regards
Vineela

Believe me, there are a lot of mock exams out there that have incorrect answers. Mock exams are nice, but they should not be treated as gospel (or replace your holy book of preference here ).
Corey
 
GeeCON Prague 2014
 
subject: string pool literals are not garbase collected ??