• Post Reply Bookmark Topic Watch Topic
  • New Topic

gc eligible objects  RSS feed

 
sree vankadara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class CardBoard {
Short story=5;
CardBoard go(CardBoard cb){
cb=null;
return cb;
}
public static void main(String args[]){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
System.out.println("done!!!"+c1 +c2+ c3);
//end
}
}

how many objects are elible for garbage collection when it reaches end. Also could you pls explain the reason.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12559
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the JavaRanch!!!

We generally don't just hand out answers here. we want to help you learn. so, you tell us how many you think are eligible - which ones and why they are or are not, and we'll tell you if you are right or not. You'll learn much more this way, trust me.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the book you got this from (SCJP 5.0 Study Guide by Kathy and Bert) explains this well enough.
 
sree vankadara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, first of all, thanks a lot for correcting my procedure to learn.

When I executed and printed the value, c1 and c3 are shown as null. I did not get why though c2 is not null even after nullifying the reference.

the book which I am going through says that there are 2 objects eligible for gc, c1 and its wrapper Short.

I am trying to understand why not c2 and c3.
 
Brent W Farrell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
References are passed by value. So the go method won't change what c2 is pointing to.
 
sree vankadara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. So if I explicitly make c2 null then 4 objects are eligible for gc. Right?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't read the SCJP Study Guide, but this Java World article has a very nice explanation of how java passes method arguments by value.
 
sakthi karthik
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i think 2 objects are eligible for gc even if we explicitly null c2.
because only two objects were created in that program.

am i right? tell me if am wrong

regards
sakthi
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one clear me about passing objects.
I understand objects are passed by Value.
So even if the copy of the refernce variable is sent , wouldnt it still be point to the same object.
And tht any manupulation of the variabel will resutl in the change of the object.

In this case isnt c2 and cb are two refernce variable pointing to the same object.
and any change to cb would result in the change of c2.
So how come c2 is not null.
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree, this example has an error in it. Check the errata thread.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Irina here we Short story=200 is just creating an reference variable of type Short right? Actual object of type Short is not created. So only one object c1 is eligible for GC. Am I right??? correct me if I am wrong.

Thanks
Srividhya
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
Short is creating an object and not just a variable.
Wrapper type create object with out using keyword "new".
So ther are 2 objets for GC
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this case isnt c2 and cb are two refernce variable pointing to the same object.
and any change to cb would result in the change of c2.
So how come c2 is not null.


I got same point with Nabila, anyone please clarify this. thank you in advance.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every one,
I want to throw some points on this. First of all object pointed by c2 reference is not eligible for GC. I think only one object is eligible for GC. Instance variable stroy is not eligible for GC. See the out put of the below code.


Value printed at line 1 is true. that means only one Short object is created. So, since c2 object is still not Garbage collected, we can refer to that Short stroy object using c2 reference. Finally, i conclude only one object is eligible for GC.

Now consider the output of this program:


Now Value printed at line 1 is false. So there are 2 Short story object created one for c1 and other for c2. As c1 is eligible for GC 1 short story object is also eligible for GC. So, a total of 2 objects are eligible for GC.

I can't figure out why if the value of short is >= 128, 2 objects are created. Can any one tell what is happening with Short variable and it's value.
 
sree vankadara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
sakthi karthik, the second object that was referred when only c1 is made null is about the Short wrapper object. So I had counted as 4 objects when c2 is made null explicitly.

However, as explained by kesava narayana I understand that there will be only 3 objects created(object refered by c1,object refered by c2, Short wrapper object). Both short references refer to the same object as it has same value.
So according to the initial program, only 1 object i.e. object refered by c1 is eligible for gc.

Nabila Mohammad, though objects are passed by value, if we change the value of instance variable or manipulate any data which the reference variable is referring to, the values are changed (Because copy of reference variable and reference variable pass are same i.e, both are refereing to the same object.) However, if we change the value of reference variable itself it is as good as changing the value of the copy and refernce to the object is lost but does not change the object value. So changing the values refered is differnt from changing the reference.
The link suggested by Stevi Deter helps in understanding it.

Pls correct me if I am wrong. Also, I did not get what error are they refering to in this question (errata thread). Anyone pls clarify.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Sree, got your point.An important one too!!!

Kesava has thrown light on a importann aspect of Short and has an intersting question of only one Short object being created which should not be avaialable for GC as it is being referred by c2.
Does any one have answers?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kesava,

Check out K & B chapter 3 .


Two instances of the following wrapper will be == when their primtive values are same.

Boolean
Byte
Characrter \u000 to \u007f
Short and Integer from -128 to 127.

This is inorder to save memory.
Hope that helps.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kesava narayana:
I think only one object is eligible for GC. Instance variable stroy is not eligible for GC. See the out put of the below code.


Value printed at line 1 is true. that means only one Short object is created. So, since c2 object is still not Garbage collected, we can refer to that Short stroy object using c2 reference. Finally, i conclude only one object is eligible for GC.



I thought a lot about this Short object.
Came to the conclusion that there are two reference variable pointin to the same Short object.
ie c1.story== c2.story;

However,when c1=null, that means
c1.story will also be null as it has no reference from c1.
But c2.story will not be null as the object still exists.
The reference variable c1.story is being assigned null and is not manupulating the object referred by c2 in anyway, so that does not make c2.story null.

So we have 2 objects for GC.

Some one correct me if I am wrong.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two reference variable pointing to the same Short object.
c1.short and c2.short are pointing to same object.
If one is assigned to null, othere will be still pointing to same object.I guess there is only one Short object created. So only 1 object is garbage collected. Any one can correct me if i am wrong.
 
sree vankadara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I think is that c1.story reference cannot be made as the object is null. As disussed earlier, both c1.story and c2.story refer to same short object.since c2 object reference still exists, short is not eligible for gc.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sree vankadara:
class CardBoard {
Short story=5;
CardBoard go(CardBoard cb){
cb=null;
return cb;
}
public static void main(String args[]){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
System.out.println("done!!!"+c1 +c2+ c3);
//end
}
}

how many objects are eligible for garbage collection when it reaches end. Also could you pls explain the reason.


There are only 2 objects created in the complete program and only 1 is available for garbage collection when we reach end.

Coming to the details:
1) A new cardboard object is created with C1 as the reference
2) A new cardboard object is created with C2 as the reference
3) For reference C3, go method of C1 is called on C2. In the go method, a new reference for C2 object is created but no new objects are created.

4) coming to Line 4 of the main method, the C1 reference is made null which means that the object that C1 is referring to is ready for garbage collection.


hope this helps
 
sakthi karthik
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its clear and i agree with you but what about that object story.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please, a Bartender come to the rescue...
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

just add that I clear with pass by value. In the go method it just pass copy of reference of c2 as parameter and return it null. But reference of c2 itself untouched or won't affected.Campfire stories about pass by value give great explanation about it.

for object story I agree that it won't be GCed since C2 reference still exist. Please feel free to correct me.
 
Bert Bates
author
Sheriff
Posts: 8948
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! This one mock question has caused so much discussion over the last year or so

Okay, first off, I'll apologize again - the question should read:

Short story = 200;

(This known issue has been on the errata list forever!)

This is so that, for sure, the object of type Short isn't going to be in a pool somewhere - in other words, for the purpose of this question, this object should be considered to have all the characteristics of normal, non-pooled, GC-eligible types of objects.

With that said, what would be the consensus about the answer to this question?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bert,Dont be so merciless .You just gave the errata with out the answer.
Can you imagine the extent of confusion it's causing digging deeper and deeper...

Can someone please clearify this.

if Short s = 200
Does that create two objects , one for c1 and another for c2
or is it a single object being shared by both?

And if 2 objects are being created, would assignign c1=null, automaticallly make c1.story null while the c2.story still exists.

I know there is only one object created if the value is between -128 to 127.



[ April 23, 2008: Message edited by: Nabila Mohammad ]
[ April 23, 2008: Message edited by: Nabila Mohammad ]
 
Bert Bates
author
Sheriff
Posts: 8948
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about it being of type Short, if you want to you could say, instead of:

Short story = 200;

how about:

MyVeryOwnClass mvoc = new MyVeryOwnClass();

of course we'd want to add a declaration like:

class MyVeryOwnClass { }

okay, now, carry on
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case it would be one object

[ April 23, 2008: Message edited by: Nabila Mohammad ]
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nabila Mohammad:
In that case it would be one object


And that would be? c1? What about Short(200) or MyVeryOwnClass that c1 had?
There is a pretty clear explanation in the K&B book:
Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

[ April 23, 2008: Message edited by: Irina Goble ]
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bert & Irina,

good hint. Short story is a instance member, whenever we created object CardBoard, it also create Short story. But when c1=null, object story that associated with C1 also GCed because no live thread can access it.
Please correct me if I'm wrong.
 
sakthi karthik
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sree vankadara:
class CardBoard {
Short story=5;
CardBoard go(CardBoard cb){
cb=null;
return cb;
}
public static void main(String args[]){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
System.out.println("done!!!"+c1 +c2+ c3);
//end
}
}

how many objects are eligible for garbage collection when it reaches end. Also could you pls explain the reason.


4 objects created in the complete program and 2 are available for garbage collection when it reach end.

cardboard object with refernce c1 and story object associated with c1
cardboard object with refernce c2 and story object associated with c2
when c1 = null, the carboard object referred by c1 got abandoned and that object and associated story object are ready for garbage collection.

please correct me if am wrong
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I think...
Let seee what other's have to say for this.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too agree with the point that there are 2 objects available for garbage collection when the execution comes to the end location.
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sakthi karthik:
please correct me if am wrong

It depends on the value of Short story. If the value is in the "-128 to 127" range, then story will refer to a cached wrapper object which will not be eligible for gc.
 
sakthi karthik
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Irina Goble

regards
sakthi
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!