Basic Question

  • Thread starter Thread starter Gaurav Vaish
  • Start date Start date
G

Gaurav Vaish

How do you force destruction of an object?

Will a simple "someObj = null" suffice?

-----
MyType someObj = new MyType();
...
...


someObj = null; // <= Will this call the destructor?

-----

--

Happy Hacking,
Gaurav
http://gvaish.virtualave.net
--------------------------------
 
Gaurav Vaish said:
How do you force destruction of an object?

You don't.
Will a simple "someObj = null" suffice?

No:

1) There may be other references to the object
2) The object won't be garbage collected until the garbage collector
next collects the generation the object lives in.
-----
MyType someObj = new MyType();
...
...


someObj = null; // <= Will this call the destructor?

No.
 
If there are no other instances of the object then this should suffice.. am
I right?
 
Gaurav Vaish said:
If there are no other instances of the object then this should suffice.. am
I right?

No. "No other instances of the object" doesn't make sense - an object
*is* an instance. Did you mean if there are no other references to the
object? If so, that's enough to make it eligible for garbage collection
- but it doesn't mean that it will instantly be garbage collected, or
that the finalizer will immediately be called.

Also note that if it's a local variable, or if it's an instance field
of another object which is going to become eligible for garbage
collection, you don't need to set it to null.
 
Back
Top