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 garbage collection problem 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 "garbage collection problem" Watch "garbage collection problem" New topic
Author

garbage collection problem

Sambit Banerjee
Greenhorn

Joined: Jul 29, 2008
Posts: 18
class Garbage
{
Short s=8;
Garbage go(Garbage gg)
{
gg=null;
return gg;
}
public static void main(String[] h)
{
Garbage g=new Garbage();
Garbage g1=new Garbage();
Garbage g2=g.go(g1);
g=null;

//method

}
}

when //method is reached how many objects are eligible for garbage collection?

can anyone give me the answer with detail explanation....thanks.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Why don't you tell us what you think the answer is before asking ...


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Trivial nitpick: Please use CODE tag to post the code snippets, which makes it easier to read.

When "//method" is reached only the object "Garbage g = new Garbage();" is ready for garbage collection.

Here important thing to understand is the when you pass a Reference type as a parameter, actual reference itself is not passed but the copy the reference.

So in method "go", you would be nullifying the copy of the reference and not the original reference "g1" itself.
Sambit Banerjee
Greenhorn

Joined: Jul 29, 2008
Posts: 18
Actually the answer given is 2.
the object g and the Short wrapper object.
I am confused about the Short wrapper object..
And what does the refernce variable g2 refer to now?
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

If Short value is 128 or more than 128
then the answer is 2 objects eligible for garbage
otherwise answer is 1 object eligible for garbage
because wrapper classes are handled like string pool


Please notify if I am wrong somewhere

Regards
Ninad


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
I have yet another question about this question...

Where did this question come from?

We are happy for ranchers to post mock questions for discussion, but we ask that you always cite the author of the question.

Thanks!

Bert


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

Joined: Aug 21, 2008
Posts: 6
Originally posted by Ninad Kulkarni:
If Short value is 128 or more than 128
then the answer is 2 objects eligible for garbage
otherwise answer is 1 object eligible for garbage


Hm, I don't really think this is right. I think what you mean is that two objects (eg. of type Short) are equal when their values are smaller than 128 (in the SCJP Exam study guide from Bates and Sierra, this is explained on page 236).

However in this case, g is eligible for garbage collection which makes 1 item. g has a reference to object s, which is a Short. Short is an object (unlike short, and regardless whether its value is above or below 128). Since this object also cannot be reached anymore, it is also eligible for garbage collection, which makes 2 objects in total.

The question is equal to selftest question 2 of chapter 3 in the SCJP study guide mentioned above...


Well....this is how I *think* it is. Please - correct me if I'm wrong!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Originally posted by Bert Bates:
I have yet another question about this question...

Where did this question come from?

We are happy for ranchers to post mock questions for discussion, but we ask that you always cite the author of the question.

To emphasise what Bert says: We require if you copy a question from a book, mock exam or other source that you quote your sources, which means you are required to say where you copied it from. (If you don't, then I'll have to delete the whole topic).

So, please tell us where you copied this from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sylvia Trommer
Greenhorn

Joined: Aug 21, 2008
Posts: 6
as I just mentioned :
The question is equal to selftest question 2 of chapter 3 in the SCJP study guide from Kathy Sierra and Bert Bates (page 259). He only replaced CardBoard with Garbage and c1, c2 and c3 with g, g1 and g2.
Sambit Banerjee
Greenhorn

Joined: Jul 29, 2008
Posts: 18
Bert and Jesper,I am sorry for the post.well as Sylvia says this is the same question that is there in chapter 3, Question number 2,page-259(scjp exam study guide,kathy sierra & Bert Bates) .I just changed CardBoard to Garbage,and c1,c2,c3 with g,g1,g2 respectively.
Again Sorry for the post but i would like Bert to help us.thanks.
And special thanks to Sylvia.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
The first version of this question used '5' as the value of the Short object. A long time ago we updated the question so that the value of the Short object was 200. We did this because wrapper objects with small values can be treated differently by the GC than other objects.

So assume that the value of the Short object is 200 and then tell me if the question is still confusing.
Sambit Banerjee
Greenhorn

Joined: Jul 29, 2008
Posts: 18
Bert,
all i could understand is that previously it was Short story=5; later you changed that to Short story=200; and that I should consider Short story=200;
am i right or its something else?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Sambit -

Correct - just take the original question and make it:
Short story = 200;

Then attempt to answer the GC question.
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
If the value of s is 200,
there should be two objects eligible for GC, they are s and g.
g2 is not eligible, because there's no object g2 instantiated, nor it refers to an object.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Originally posted by Bert Bates:
The first version of this question used '5' as the value of the Short object. A long time ago we updated the question so that the value of the Short object was 200. We did this because wrapper objects with small values can be treated differently by the GC than other objects.


does this means that if the value was 5, then the Short object will not be garbage collected or is the behaviour unpredictable???


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
if the value was 5 the question wouldn't be on the exam
ankana mukherjee
Greenhorn

Joined: Aug 22, 2008
Posts: 6
what would have been the problem if the value is less?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Originally posted by Bert Bates:
if the value was 5 the question wouldn't be on the exam


is this some sort of a signal that these type of questions are not on the exam???
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
you guys need to relax a little

If Short story = 5 then there are issues with how the GC interacts with constants pools. If Short story = 200 then you can focus on how the GC works for 99% of objects. The bottom line is that you need to understand how the GC works in general, but you DON'T need to understand how it works in regards to constants pools.

hth,

Bert
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

I found a question which was like this on javablackbelt.com

int i1 = 10;
Integer i2 = new Integer(10);
if(i1==i2)
{
System.out.println("Equal");
}

The answer is equal...First of all I don't get why the answer is true. Is it because of this

if(Integer.valueOf(i1)==i2)

The autoboxing code will look in the pool of objects and found an Integer object with the same value in the range -128 to 127 and didn't create a new Instance...

As Bert is saying then there is no need to worry too much about constant pools. So do I need to worry about these kind of questions in Mock exams available over the internet...
John Sutt
Ranch Hand

Joined: Mar 07, 2008
Posts: 42


Ankit, are you sure the left side is boxed?
I thought that, if one side of the == is a primitive,
the right side would be unboxed and then, ofcourse,
the expression is true.
I'm not sure though.


SCJP 6 (91%), SCJD (91%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Originally posted by John Sutt:


Ankit, are you sure the left side is boxed?
I thought that, if one side of the == is a primitive,
the right side would be unboxed and then, ofcourse,
the expression is true.
I'm not sure though.


if you change it to if(i2==i1) then also the answer is the same.....
John Sutt
Ranch Hand

Joined: Mar 07, 2008
Posts: 42
Oh no, it has nothing to do with left or right.
I meant, if one of both sides -either left or right-
is a primitive, the other side will be unboxed.

So this is contrary to your previous message, where you assumed that i1 would be boxed.
Hope you understand what I mean.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Hi Sambit,
I suggest you do a "Search" in the javaranch forum for previous posts on this question and you will find very good explanation. I did that.
Sirisha


If you worry you cannot work... If you work you need not worry
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Careful guys!

I said you wouldn't have to worry about constants pools with regards to the GC!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Originally posted by John Sutt:
Oh no, it has nothing to do with left or right.
I meant, if one of both sides -either left or right-
is a primitive, the other side will be unboxed.


Yes you are right....This question is not about constant pools....the Integer object will be unboxed and the int variable will not be boxed.......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection problem