LWN.net Logo

C#/Java have destructors...

C#/Java have destructors...

Posted Mar 30, 2005 10:57 UTC (Wed) by pkolloch (subscriber, #21709)
In reply to: OO is yesterday's news. by khim
Parent article: Evidence that Python is advancing into the enterprise (NewsForge)

...but there are only guarantees to when they are not called. (Well, I have to admit that I have not dug deeply into this explicit GC control, but that's probably out of scope. In some circumstances even (managed) C# might even make some guarantees.)

In C#:

~TypeName() { ... }
In Java:
protected void finalize() throws Throwable() {...}

While I agree that you should not use that in pure managed code, it might make a lot of sense in wrappers to native code which allocates resources (which you want to deallocate automatically). In C# 2.0 you can even specify some kind of implicit cost for classes to provide meaningful heuristics for this case.


(Log in to post comments)

C#/Java have destructors...

Posted Mar 30, 2005 11:48 UTC (Wed) by phgrenet (guest, #5979) [Link]

I think one problem with Java and C# is that they only allow allocation of objects on the heap. If you can allocate your object on the stack then there is no need to control what the GC is doing because you know exactly when the object will be deallocated. C++ and Lisp give you that choice.

C#/Java have destructors...

Posted Mar 30, 2005 13:44 UTC (Wed) by TImaniac (guest, #28862) [Link]

In C# you can allocate a struct on the stack instead of the heap : this struct are value-types but inherit from Object.
if you need temporary stack memory, there is also a stackalloc keyword.

C#/Java have destructors...

Posted Mar 30, 2005 14:58 UTC (Wed) by cajal (guest, #4167) [Link]

Some JVMs can allocate objects on the stack. There is much research into this area (google for "escape analysis"). But there's no word as to when Sun's JDK will support it.

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds