deleting an object in java

J

Jonas Hei

Can someone please enlighten us on what happens when an object is set to
null?

class SomeClass {
private int someData;
//more stuff - lots of memory consumed by data here
}

class SomeApp {
private SomeClass someObject = null;
public void someMethod {
someObject = new SomeClass();
//do stuff...
someObject = null; //once we're done it
//Is the above instance of SomeClass
//ready for garbage collection after
//it is set to null above???

//do more stuff...
//can do much better because we've
//(sort of) disposed off the memory
//consumed by someObject - is it true??

}
}
 
K

klynn47

It's kind of tricky. When there are no references left to an object,
then it is marked as garbage and is eligible for garbage collection.
Simply setting the reference name to null does not guarantee that it
will become garbage because there may be other references to it. When
an object is marked as garbage, and is eligible for garbage collection,
there is no guarantee that it will be collected.
 
J

Jonas Hei

Yes I realize it is a bit tricky (more so for me since we're newbies...)

I am developing embedded application (j2me) and have very small amount
of memory available.

So I would like to minimize keeping references to objects that won't be
needed. Therefore, I just need assurance that if I set the objects to
null (and ensuring that all references to objects that are not needed
are set explicitly to null) then it should be atleast marked as garbage
by GC.

I understand that there won't be any guarentees about when it will be
collected by GC. But at least there will be a guarentee that I won't get
an out-of-memory condition while there is un-collected garbage around
in memory. That is why I wanted to ensure that we set all objects (that
we don't need anymore) to null.

Am I on the right track here??
 
E

Eric Sosman

Jonas said:
Yes I realize it is a bit tricky (more so for me since we're newbies...)

I am developing embedded application (j2me) and have very small amount
of memory available.

So I would like to minimize keeping references to objects that won't be
needed. Therefore, I just need assurance that if I set the objects to
null (and ensuring that all references to objects that are not needed
are set explicitly to null) then it should be atleast marked as garbage
by GC.

I understand that there won't be any guarentees about when it will be
collected by GC. But at least there will be a guarentee that I won't get
an out-of-memory condition while there is un-collected garbage around
in memory. That is why I wanted to ensure that we set all objects (that
we don't need anymore) to null.

Am I on the right track here??

Circumstances vary, of course, but the prevailing wisdom
seems to be that it is usually not worth while to make a fetish
of setting references to null in an attempt to manage memory.
In "Effective Java," Joshua Bloch specifically recommends that
this should not be done in most cases.

There are exceptions, naturally. Bloch illustrates a Stack
class that holds references in an Object[] while incrementing
and decrementing an int index with each push and pop. The
problem is that decrementing the index doesn't remove the popped
Object's reference from the array; it just sits there until and
unless something else is pushed over it. Even if the rest of
the program loses interest in the popped Object, the Stack's
Object[] still holds a reference to it and prevents it from being
collected. In a situation like this, nulling the array element
is the right thing to do.

But for method variables, it's usually better to let a
reference go out of scope when you no longer need it -- if you
don't need it, you're better off not having it at all than
setting it to null and dragging it along like a tin can behind
the newlyweds' getaway car. If your methods tend to be simple
and short (so the complexities of your design appear as the
composition of many simple parts rather than as an uneasy alliance
of a few ungovernable super-methods), the management of local
variable scopes should be pretty much automatic.

YM(emory)MV.
 
J

Joona I Palaste

Yes, 'nulling' is enough, to mark the object for GC.

It is enough, but not necessary, for no references anywhere to exist any
more to the object. What is specifically needed is that no chain of
references starting from any live thread, and ending at the object,
exists.
This can be remembered by reciting some programming Zen koan I read
somewhere. It went something like this:

One day a student came to a Zen guru and said: "I know how to make the
perfect garbage collector. All I need is to count the references to an
object and if the count is null, the object is garbage collected."
The Zen guru replied: "One day a student came to a Zen guru and said:
'I know how to make the perfect garbage collector. All I need is...'"
At that point, the student was enlightened.

Moral: Pure reference-count garbage collectors have trouble with objects
referring to themselves.
 
M

Mike Schilling

Jonas Hei said:
Can someone please enlighten us on what happens when an object is set to
null?

You're getting good advice; I won't try to improve on it, but you're using
misleading terminology.

What you set to null isn't an object, but a reference. In

Object o = new Object();
Object p = o;
p = o = null;

there is only one object. First, the reference o points to it, then two
references point to it (o and p), and finally no references point to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top