Re: how to destroy object from inside?

From: Andy Fish (ajfish_at_blueyonder.co.uk)
Date: 05/06/04


Date: Thu, 06 May 2004 12:09:07 GMT

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

"vertigo" <none@microsoft.com> wrote in message
news:c7cq28$n77$1@atlantis.news.tpi.pl...
>
>
>
> > 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
>



Relevant Pages

  • do not cast the monarchys perfectly, sleep them yet
    ... manuscript. ... If you'll terminate Osama's cult with prides, ... Why will we borrow after Katya ignores the spanish function's ... Plenty of references ...
    (sci.crypt)
  • Re: How do you kill a completely locked up thread?
    ... terminate a process any more than you can safely abort a thread. ... you can manage your external resources in such a way that it's ok for the ... being left in an inconsistent state. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Threading, and suspending or killing a thread
    ... Other threads that are spawned like this go right back to zero CPU, ... the Process Explorer to kill it there. ... When you terminate a thread in a .NET application you should consider the whole appdomain doomed, as in, you should no longer keep it around. ... - deadlocks, if the thread locks internal resources it will never unlock them, thus blocking future threads from ever accessing the resources the lock protects ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why leave the error handling to the caller?
    ... does terminate with an error message on fail. ... Library routines have no business deciding to terminate ... That's rubbish because the above situation is on a modern medium to high spec notebook. ... On my previous company notebook which was high spec at the time I would also run out of resources. ...
    (comp.lang.c)
  • Re: Garbage Colletor
    ... People who know a lot more about the garbage collector than I do have in this very newsgroup provided very good explanations as to how very efficient garbage collection actually is. ... The bottom line in any case is that you have no choice but to release your resources explicitly if you can't wait for the GC to invoke "Finalize". ... if your object references only managed data, then the instant your object is no longer referenced itself, ALL memory resources referenced by that object are also no longer referenced, and are released in the same instance the root level object is. ... They exist for explicit releasing of resources, but they aren't needed unless you a) want to keep the object for later reuse, but release resources or b) you want to ensure that unmanaged resources are also freed. ...
    (microsoft.public.dotnet.languages.csharp)