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 ExamLab - garbage collection (3rd test, question no. 19) 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 "ExamLab - garbage collection (3rd test, question no. 19) " Watch "ExamLab - garbage collection (3rd test, question no. 19) " New topic
Author

ExamLab - garbage collection (3rd test, question no. 19)

Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi folks,

I've tried to figure out how many objects are eligible for GC.
Below, please find the code which I've modified a bit in order to see WHICH objects are eligible:


The correct answer is 2 - and indeed, running the code above, I get object 3 and 4 beeing eligible for GC.

Nevertheless, I've tried in vain to figure out why.
To resolve this, I've painted the following diagram:



In my opinion, object 3 and 4 would be isolated (and thus eligible for GC) only if line 6 (coming from object 4 back to object 1) didn't exist.
But maybe, I've misunderstood a detail?
Is something wrong with my diagram? Or did I oversee something?


SCJP 6 (88%), SCWCD (89%)
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Your diagram looks fine to me...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Thank you, Ankit ... it's because I want to become a new Picasso ;-))

But nevertheless, my question still remains unanswered:
why are the last two objects eligible for GC, if there's still a connectionn (line 6) from A(4) to A(1)?
Any idea?
Maren Fisher
Greenhorn

Joined: Nov 29, 2009
Posts: 7
I think (and I'm not an expert because I'm figuring this out too), it's because there is no reference from the stack. Since the s1 reference only reaches out to A2, A3 and A4 cannot be reached. Therefore they are eligible for gc.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

The line
s1.a2.a1.a2 = null;
is equivalent to
s1.a1.a2=null i.e there would be no reference to the new A(3)

The statement
s1.a1.a2.a2 = new A(4); can be
new A(3).a2=new A(4);

As new A(3) would not exist anymore on heap,
there would be no reference to new A(4) too.
THus 2 objects would be available for gc.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

why are the last two objects eligible for GC, if there's still a connectionn (line 6) from A(4) to A(1)?

As Maren said, the connection 6 doesn't let you access the last two objects from your code. The last two objects are inaccessible now, thus they are eligible for GC...
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Thank you, Ankit, Mohitkumar and Maren!

Now I think I'm able to locate the main issue: line 6 goes back from A(4) to A(1) (arrow pointing to the left), but it deals only with a "passive" connection.
It seems that with object references, it behaves according to the famous Hollywood principle: "don't call us, we call you".
Consequently, the JVM doesn't care about line 6 - if line 6 pointed into the opposite direction, instead, A(4) would not be eligible for GC.

Hope I'm right ... when this thread is closed, I'll remove my painting above, trying to exhibit it in the Museum of Modern Art ;-))
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ExamLab - garbage collection (3rd test, question no. 19)
 
Similar Threads
Garbage Collection (Cyclic references) Also known as Object Island
Garbage Collection
exam lab gc question
Doubt related to Garbage Collection Questions
Doubt over Garbage Collection