| Article: |
What I Hate About Your Programming Language | |
| Subject: | Java; I want a destructor! | |
| Date: | 2003-07-08 07:03:53 | |
| From: | anonymous2 | |
|
Response to: Java; I want a destructor!
|
||
|
Unfortunately the presence of a destructor poses HUGE problems for a garbage-collected language. The garbage collector can't tell when it's safe to collect an object, it can't just drop all the objects when the program ends, etc. I too missed destructors, but try ... finally ... is a good substitute.
|
||
Showing messages 1 through 1 of 1.
-
Java; I want a destructor!
2006-01-23 23:26:01 bastos [View]



This is why Java *does* give you a destructor. Java destructors however have a problem, and this problem is called by java optimizations. Some of these optimizations are that Java doesn't collect immediately. And java doesn't collect immediately due to the fact that it's not always effective to do so, such as if a bunch of objects were just fell out of reference because a program is about to exit, obviously we'd be freeing them up for nothing in most platforms as they'll get cleaned up by the process ending.
Also, since java allocates its memory in blocks, it will wait until a threshold is met before it garbage collects what has no reference rather than allocating more memory. This is another one of the many optimizations that has sped java up over the years.
Below is a simple program, running it and analyzing the output will demonstrate the behavior described above, and set the record straight about java having a destructor: