• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you tell us what you think the answer is before asking ...
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sylvia Trommer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sambit -

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

Then attempt to answer the GC question.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the value was 5 the question wouldn't be on the exam
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what would have been the problem if the value is less?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful guys!

I said you wouldn't have to worry about constants pools with regards to the GC!
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.......
 
Grow a forest with seedballs and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic