• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your diagram looks fine to me...
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;-))
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic