aspose file tools*
The moose likes Beginning Java and the fly likes question on Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on Garbage Collection" Watch "question on Garbage Collection" New topic
Author

question on Garbage Collection

Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

This Question is from Kathy Sierra page 269 chapter 3.





when //doStuff is reached,how many objects will create and how many objects will be eligible for GC?? Please give proper explation,if possible explain with the diagram...Please...
Kristjan Toots
Ranch Hand

Joined: Jun 03, 2011
Posts: 59

Hi mate,
Please use some code tags.


Please correct my english.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And please QuoteYourSources.

Also I'm sure I've seen that exact piece of code posted here before. If you search the forums for "CardBoard" there's a good chance you will find it.
Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

I am unable to understand from previous posts..Can you please explain it for me...please...
whether 3 objects will be created or 4?? explain how???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I'm going to have to delete this post if you don't tell us the source of this question.
Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

This Ques is from Katthy Sierra page 269 chapter 3.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Hi,
c1 is null ..this is very explicit.
c3 is also null since the object reference returned by the method 'go' is null;

C2 is pointing to a object on the heap. So C2 is not available for GC.
So only c1 and c3 are available for GC.

You may get confused as to how c2 is not null since it is passed as an argument to the method 'go' which sets it to null.

It must be understood that c2 is a reference to an object and not the actual object itself.
Also in java it is always pass by 'value' ..

So when pass c2 as the parameter to the 'go' method, the jvm creates a copy of 'c2' and passes it to the 'go' method.

c2 -> {some object on the heap }
jvm creates a copy of c2 ..and names it cb.
so cb is also pointing to the same object that c2 was pointing to..
inside the method you are doing 'cb = null ' which affects cb only and not c2 .
c2 continues to point to the same object as it was earlier.

so except for c2 other references are null which means they are not pointing to any object and hence eligible for garbage collection.




Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

Thanks Yogesh Gnanapraksam,it was very helpful for me...thanks a lot....


Now i just want to know only one thing that,,will line CardBoard c3=null(generated by CardBoard c3=c1.go(c2)) will create any object in the heap or not? And you also have not given the ans that total how many objects will create in the heap.
So please reply..
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Now i just want to know only one thing that,,will line CardBoard c3=null(generated by CardBoard c3=c1.go(c2)) will create any object in the heap or not?



Just think about what happens during runtime..

CardBoard c3 = cb; ( the reference returned by the go method) .
cb is not pointing to any object on the heap ...consequently c3 will also not point to any object on the heap.

Forget about object creation,for garbage collection you need to know whether the reference (c3 in this case) is pointing to any object on heap. In this case it is not ,so it is eligible for garbage collection.
You create objects by using the 'new' operator only.
In this code the new operator is used only twice..so only two Cardboard objects are created...

A simple way to check this is to have a static variable in your class and increment it in the constructor..


You should try running the program with System.out.println() at each point to know what is happening..


You are welcome..

Regards
Yogi



 
 
subject: question on Garbage Collection