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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Given in Mind Q's mock exam..
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
The ans is b.(1 object).
My ans was 'a'. I thought name, newname and newestname points to the same string in the literal pool and that GC is also concerened with Heap. Am I right? If not, which object is eligible for GC?


Thanks,<br />Lakshmi.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
I think one object is eligible for garbage collection.

The object created in the second line of the code has no reference so that is eligible for garbage collection.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
Please clarify if mu understanding is wrong.
Thanks
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
could anybody tell me wheather i am right or wrong?
garbage collection always troubles me!
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hi Mrudul,
I think u r absolutely right,.
The String object created in line 2 is eligible for GC.
Good luck,


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Puneet Agarwal
Ranch Hand

Joined: Jul 15, 2003
Posts: 49
I am not so sure .
Why wouldnt the object "name" be not elegible for GC .
Sripada Phani
Greenhorn

Joined: Nov 28, 2003
Posts: 7
Any object will get eligible for garbage collection only when the last reference to the obejct is removed.
In this case eventhough reference name is made null, the other referene newestName still pointing to the object. So, the object that was pointed to name was not garbage collected.
Also, all the references will be stored in stack and all the objects that are created by new keyword will be place in heap. When you assign any new reference ( by creating new object), automatically the old reference will be made eligible for garbage collection.
I hope this would answer you question.....
Puneet Agarwal
Ranch Hand

Joined: Jul 15, 2003
Posts: 49
Just a small clarification i need . I guess it is the objects which are made avilable for GC and not references , right . When I say something like String str = "abc" , I am creating a new object right and a reference is getting assigned to it . Now if I say that str = "def" , then "abc" object will be avilable for GC , right...
Sripada Phani
Greenhorn

Joined: Nov 28, 2003
Posts: 7
You are right.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi everyone,
Puneet is very right. But let me add something to it (if I am not mistaken), String object is not mutable (read only). So when newName assigned to 'Jason', at line 3, it creates a new object and the newName was being referenced to it. Thus, the previous object, 'Nick', was made available for GC.
Hope my explanation is correct. If it is wrong, my apologies.
CH
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Thank you all..
Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
well 2 objects will be eligible for Garbage Collection
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hello everyone!
I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!
Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Thnak you Bert for letting us know the Exam objective.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Originally posted by Bert Bates:
Hello everyone!
I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!
Bert

Hi Bert,
Just out of curiousity, could you tell us, at least me, how many object will be GC?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
CH -
If you restate the question without using String objects, I'll give you my opinion
Bert
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Originally posted by Bert Bates:
CH -
If you restate the question without using String objects, I'll give you my opinion
Bert

Hi Bert,
It's OK then. Just curious to know something out of the scope of SCJP exam.
Thanks.
Cheers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection