aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Garbage Collection Problem" Watch "Garbage Collection Problem" New topic
Author

Garbage Collection Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



When // doStuff is reached, how many objects are created within main() {specify them} and which one's are eligible for GC?

i think 4 objects would be created i.e.
-c1 and Short variable contained in it
-c2 and Short variable contained in it
----only c1 and it's instance variable Short would be eligible for GC
but i have one doubt would c3 be eligible for GC too ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Please QuoteYourSources.


[My Blog]
All roads lead to JavaRanch
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Also do you have any guesses of your own about how many objects are eligible for GC??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
dipayan chatterjee
Ranch Hand

Joined: Oct 03, 2007
Posts: 47
Hi Mohit

i agree with Ankit , first try out the problem yourself and post what you think the answer would be, we can then identify where you went wrong( if your answer is wrong) in this way you will learn more buddy. so post your answer and we are always there to help you out


SCJP1.4 , gearing up for SCWCD 5
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

answer posted
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

mohit your answer is right. 4 objects were created and 2 were eligible for GC. As far as c3 goes, it never pointed to any object, it was null. c2 will not be eligible as passing it to go method doesn't makes it null (read this for more info)...
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

IF I REPLACE THE LINE
Short story = 200;
WITH
Short story;

then ,would there be 4 object created or 2 objects as no value would be assigned to story instance variable ?
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

If you don't assign story any value, then it won't point to any object so only 2 objects will be created (as you said yourself)...
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Garbage Collection Problem
 
Similar Threads
GC help
object eligible for GC
Garbage Collection
Confused with GC related K&B 1.5 question
garbage collection?