how to destroy object from inside?

V

vertigo

Hello
i have:
public class MyClass{
......
public void finish(){...}
}

How can i destroy Myclass object from finish function ?
I want to be sure that JVM will free memory allocated for that
class.


Thanx
Michal
 
J

Joona I Palaste

vertigo said:
Hello
i have:
public class MyClass{
.....
public void finish(){...}
}
How can i destroy Myclass object from finish function ?

You can't.
I want to be sure that JVM will free memory allocated for that
class.

You must get rid of all references pointing to your MyClass object. That
will tell the JVM it *can* free the memory allocated for that object.
When it *does* free the memory might be right then, or at any moment
after that.
 
V

vertigo

You must get rid of all references pointing to your MyClass object. That
will tell the JVM it *can* free the memory allocated for that object.
When it *does* free the memory might be right then, or at any moment
after that.

but i do not have any references to that object.
Instead it has references to other objects (other classes).
It's because this class is in fact some sockets which
receives data and prints in other window (other class).

How can i destroy it ?

Thanx
Michal
 
J

Joona I Palaste

but i do not have any references to that object.
Instead it has references to other objects (other classes).
It's because this class is in fact some sockets which
receives data and prints in other window (other class).
How can i destroy it ?

You don't. The JVM will do it for you, when it finds that you no longer
have any references to it. If the references in MyClass are the only
references to your other objects, the JVM will destroy those objects
too.
 
R

Roedy Green

How can i destroy Myclass object from finish function ?
I want to be sure that JVM will free memory allocated for that
class.
You can't destroy a object. All you can do is set all references to
that object to null.
 
R

Roedy Green

but i do not have any references to that object.
Instead it has references to other objects (other classes).
It's because this class is in fact some sockets which
receives data and prints in other window (other class).

How can i destroy it ?

If there are no references to it, it is already destroyed. Its ram
will be collected on the next GC sweep, as will any objects is points
to that have no other references.. There is no way to get the ram
recycled without a GC sweep. You can call System.gc(), but there is
no point. It will call it when it runs out of RAM.

Read about how GC works. See
http://mindprod.com/jgloss/garbagecollection.html
 
A

Andy Fish

I'm guessing maybe your problem is that you want to explicitly close or
terminate other resources owned by your class without waiting for the
garbage collector to free the memory.

e.g. say your class has a file or a socket open, or a datbase connection or
owns a window or something like that.

in this case, include a terminate method in your class which explicitly
closes those resources. obviously you need to make sure you don't ever
access the object after you have called the terminate

don't rely on the finalize() method to do these things unless you really
don't care when they get closed. Given that file handles and database
connections are limited resources, you should always care about when they
get closed unless you're just about to exit the JVM.

In my experience of java programming I have never yet written a finalize
method.

Andy
 
S

Steve Horsley

vertigo said:
but i do not have any references to that object.
Instead it has references to other objects (other classes).
It's because this class is in fact some sockets which
receives data and prints in other window (other class).
Oh yes you do!
The object has a reference to itself (called "this").
Combine that with the fact that any object which is "reachable"
by a thread is not eligible for GC, and you find that any
object is ineligible as long as there is a thread inside
any of its methods.
How can i destroy it ?

The best you can do is to release any non-memory resources (e.g.
this.close()) and then forget about it. Forgetting about it involves
making it unreachable, which involves removing any references to it
from any other reachable objects, and letting all its methods terminate.

Steve
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top