File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Doubt in Garbage Collection

Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
I have a doubt in Kathy Sierra book(Chapter Assignments)

The question is as follows

class a
{
Short s;
a go(a b)
{
b=null;
return b;
}
public static void main(String args[])
{
a a1=new a();
a a2=new a();
a a3=a1.go(a2);
a1=null;
}}
How many objects will be eligible for Garbage collection?

The answer is given as 2(c1 and the associated Short wrapper )

My doubt is since a3 reference has been allocated a null object will it not be considered for garbage collection?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lavanya Raguram:
...since a3 reference has been allocated a null object will it not be considered for garbage collection?

Welcome to JavaRanch!

Note that a3 is assigned a null reference, but it never actually points to an object.

Similarly, the instance variable s is automatically initialized to null whenever an instance of 'a' is created, but there are no Short objects created.

I only see two objects created here: An instance of 'a' assigned to a1, and another instance of 'a' assigned to a2.

When a1 is later reassigned to null, the object it was referencing becomes eligible for garbage collection. The other instance of 'a' will be eligible when the main method exits (and the local variable a2 no longer exists).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
hi marc weber
if the qustion is
How many objects will be eligible for Garbage collection after the line a1=null?

your answer is only one Object ?

more question
if I use the line Short s=12; insteading of the line Short s;
what happen to the answer?
I think Short s=12;maybe will create Short Objet and reference in the Constants Pool
which will pervent ths Short Object be collected?
so the answer is also only one ?
correct me if I mistake?thanks!
[ April 04, 2006: Message edited by: Changchun Wang ]
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Hi Changchun,

You are right only one object will be garbage collected.

Gaurav
Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
As u said if it is Short = 5; then wen a2 is assigned null then the corresponding short wrapper object asssociated with it will also become null na so will it not be 2
correct me if i'm wrong
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

[ April 05, 2006: Message edited by: Naresh Kumar ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi guys,

Man this errta has caused a lot of grief! When we wrote this question we didn't mean to have the so-called "wrapper pool" be part of the question! Sorry! :roll:

Remember - the real question in the book assigns a value to the Short.

We've posted a temporary errata page on this forum, and there you'll see that we've changed the value of the Short so that it won't be in the pool...does that answer the question?

Bert
[ April 05, 2006: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Bert Bates:
...Remember - the real question in the book assigns a value to the Short.

We've posted a temporary errata page on this forum, and there you'll see that we've changed the value of the Short so that it won't be in the pool...does that answer the question?
...

That clears it up! I was really wondering how a Short instance found its way into this answer.

If the Short is instantiated (with a "non-pool" value), then the number of objects eligible for GC after the line a1 = null is 2 (the object previously referenced by a1, and the associated Short object).
Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
We've posted a temporary errata page on this forum

What is this errata page i'm not able to find it n also wats the "wrapper pool" is that the memory allocated for wrapper objects or something else ?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The temp errata page is this thread.

The "wrapper pool" refers to autoboxing behavior in Java 5.0. If two equal values are within the range of a byte, then their boxed instances will actually be the same object (similar to the String pool).

However, I don't think this is an official term acknowledged by Sun, so I would keep it in quotes if you use it.
[ April 06, 2006: Message edited by: marc weber ]
Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
So if Short=5;

then it will be stored in wrapper pool as said by u since the value is less than that of bye.In this case how many objects are eligible for garbage collection at the end of this program
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Garbage Collection