This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q of GC

 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, after line "doSomething", there are 2 objects of class A and class C eligible for GC, right?
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think they are eligible for GC and first C need to be GC'ed before A.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claire,
I didn't understand your answer...
how many As and how many Cs do you think?
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Varma:
i think they are eligible for GC and first C need to be GC'ed before A.

I suppose there is one object of class A and one object of class C for GC. But is there a sequence that one should be GC'ed first before the other?
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes!! you are right Meng..there is one C and one A to be GC'ed. They should be done in the sequence (First C followed by A) because class C will have a reference to C. SO C cannot be GC'ed until that reference is removed.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Varma:
yes!! you are right Meng..there is one C and one A to be GC'ed. They should be done in the sequence (First C followed by A) because class C will have a reference to C. SO C cannot be GC'ed until that reference is removed.

I seriously doubt that this is the sequence in which garbage are collected. Perhaps if the GC is employing reference counting. But even so, it will just mark one object first before the other. But the actual reclamation of the object, I doubt that it follows the logic mentioined above.
Can someone verify this?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, one C and one A, AND Alton is correct. There is no way to know which of them will be collected first, in fact there is no guarantee that either of them will EVER be collected, and it's even possible that only one of them will be collected (although that last one is pretty unlikely).
For the exam, you only care about if and when objects become 'eligible' !!! The spec does not define when or how GC will work, and implementations may vary from JVM to JVM.
And really, in this case I'm not just being an exam hound , as a good Java programmer, you have to know what is left to the individual JVM, because you don't want to depend on the behavior of a particular JVM, it makes your code less portable! So forget all this nonsense about the GC sequence, relax, have a beer, and study the stuff you can do something about.
-Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic