permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q of GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q of GC" Watch "Q of GC" New topic
Author

Q of GC

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
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

Joined: Dec 24, 2002
Posts: 48
i think they are eligible for GC and first C need to be GC'ed before A.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Claire,
I didn't understand your answer...
how many As and how many Cs do you think?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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?


Meng Yi
Arvind Varma
Ranch Hand

Joined: Dec 24, 2002
Posts: 48
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

Joined: Aug 30, 2002
Posts: 57
Thanks,
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Oct 14, 2002
Posts: 8815
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q of GC