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?
Joined: Dec 24, 2002
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.
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?
Joined: Oct 14, 2002
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