File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection: all or nothing??? 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 "Garbage collection: all or nothing???" Watch "Garbage collection: all or nothing???" New topic

Garbage collection: all or nothing???

Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Hi there!
Question from a mock exam:

Which of the following could be a result of attempting to compile and run the program?
a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3
My answer was a, c, d, e. The correct answer apparently doesn't include c.
A1A2A3A1 is printed in the for loop in method m1. The call to System.gc() does not guarantee the gc will run, so it might be possible none of the object is gc'ed (that's answer a). Another option is they are all gc'ed and that could happen in any order. That gives you answers d and e. Cool. What happens with c? Isn't it possible that A2 and A3 are gc'ed but A1 isn't??? Why not?? It sort of implies the GC might collect everything or not collect anything at all ...
Edited by Corey McGlone: Added CODE Tags
[ March 30, 2004: Message edited by: Corey McGlone ]
Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Lionel,
Answer c is not valid because you'r dealing with so called
islands of isolation. The objects refer to each other
but there is no live reference to any of them, so they
are eligible for garbage collection as a group and no
individual object will be missed.
Very nice question, may I ask where you got it from?
And if it's available for download or something.
Gian Franco
[ March 30, 2004: Message edited by: Gian Franco Casula ]

"Eppur si muove!"
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Hi Gian Franco,
So for island of isolation the gc does work with an all or nothing policy ...
I got that question from Dan Chisholm's exan:
Another rancher sent me that link. I don't know if that is really Dan's page or is. They have different "latest exams".
I agree. Here's the link:
subject: Garbage collection: all or nothing???
It's not a secret anymore!