This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Garbage Collection

 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Changchun,

You are right only one object will be garbage collected.

Gaurav
 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ April 05, 2006: Message edited by: Naresh Kumar ]
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic