aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
Here is a question from MindQ.

How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4

My ans was 0 as there is no object created here and String literals are not garbage collected. But answer is not that. what would be the correct answer for this?

Thanks in advance.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
answer is b.

String literals are also objects.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
Aren't string literals stored in String Pool? I read that Strings in String Literal Pool are not garbage collected? Am I missing relavence to String Literal Pool some where here?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
They are stored in String pool. But they are called String literals objects.

e.g.,

String str=new String("hello");

here if "hello" is not found in String pool, then Java will create two objects.

1. String object in non-pool memory
2. String literal object in String constant pool.


Whereas if String str="hello1";

then Only one String object is created in String constant pool.

regards
Jayes Herryl
Greenhorn

Joined: Jun 09, 2006
Posts: 7
So does that mean only name can be garbage colleccted?
sri latha
Greenhorn

Joined: Mar 03, 2006
Posts: 27
Hi i have one doubt on this question.i thought the answeris c.2 objects are eligible for garbage collection.object 'nick' is also eligible for garbage collection.becaure newname refer to 'jason'.'Freida' is garbage collected because it is null.
can anyone explain wheather my guess was correct or not.
preparing for scjp1.4
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Please Check this thread.
"Nick" String Object is in the String Literal Pool.
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
Thanks to wise and Naseem, but still I am not clear about this question. Here is my explanation about this qn.

String newName = "Nick";
Nick is created in heap,with a newName referencing it.Also reference is stored in String Literal Pool table. So it has two reference, one is with newName and the other is in String Literal Pool.

newName = "Jason"; //same as new String("Jason");

Jason Object is created and its only reference is new Name.
Point to note, Nick has still one reference from string literal pool.

name = "Frieda"; //same as new String("Frieda");

Frieda Object is created and its only reference is name.

String newestName = name;

newestName also refers to Frieda Object. Hence two references to Frieda Object (name and newestName)

name = null;

name is made null, but still there is a reference to Frieda Object through newestName. hence "Frieda" is not eligible for GC.
"Jason" is referenced by newName. Hence it is not eligible for GC.
"Nick" is in String Literal Pool. That is there is a reference to Nick from a constant pool.(according to corey tip line).

Can some one point out where did I go wrong?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
I will pick up the "a) 0" answer you picked up.
Bonny Sen
Greenhorn

Joined: May 09, 2006
Posts: 26
Hi Pramila,

Your explanition sounds perfect. But here is how I feel thinds go:

The moment you do newName = "Jason"; then the String object "Nick" becomes like an orphan object having no variable referencing it. This object is simply LOST. Since now we do not have any refernce to "Nick", and so "Nick" object is eligible for garbage collection.

I am not sure whether Strings in String Literal Pool are garbage collected or not. I have to confirm on this. I belive they are GC. If not then think about a large probram where a lot of String objects are created. If GC was not possible then the String Literal Pool would have a huge collection of String objects, resulting in high consumption of memory , and the very first idea of having GC goes haywire.

So I believe that there will be 1 object eligible for GC. Please correct me if I am wrong anywhere in my explanation.


With Love, Bonny
"Life is like an ice-cream, Enjoy it before it melts..."
 
wood burning stoves
 
subject: Garbage Collection
 
Similar Threads
about garbage collection
MindQ q#36
GC question
dout regarding garbage collection
garbage collection