Re: Objects that can't be garbage collected?
Daniel wrote:
I got a weird question in a phone interview the other day that I'm not
sure what to make of.
The question was "What object(s) cannot be garbage collected?" I asked
for a little clarification, and he said "What objects, if collected,
would cause problems [in the JVM]?"
Hmmmm... still not sure if 1) I accurately understood what he was
asking 2) It's a perfectly good question that I don't know the answer
to, or 3) it was a trick question
If it's #2, hopefully someone will fill me in.
My first thoughts were that this must be a theoretical question, b/c
if there were such a situation where an object is collected before
it's safe to do so, then it would be a pretty big bug in the JVM
implementation.
Going for the theoretical angle, my thoughts were that something like
a ClassLoader object could be the answer. Let's say that somehow, I
managed to erase the reference from Foo to the FooClassLoader that
loaded it , and that it is collected before an instance of Foo calls a
method that uses an instance of Foo2 (assume that Foo and Foo2 are
from a library that only FooClassLoader has access to.) We'd have a
something like a ClassNotFoundException from the bootstrap CL,
correct? As far as the "realisticalness" of the situation, it seems
like the ability to manipulate a class's ClassLoader field would
violate the security model. Then again, if the reference in Foo to
its class loader is simply a regular private member, then Foo could
set the reference to null in its constructor or some other point,
correct?
Am I missing something obvious, or is this a weird question?
A class loader may be reclaimed, along with any class that is within it,
following the same logic as any other object. JLS 12.7
<http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7>
The only thing that I can think of is the system class loader and system
classes. Alternatively, objects that still have hard references to
them, although this kind of goes without saying.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>