This section covers some of the unique constructs of the Java language, related to memory management.
Dreaming of delete
or free
operators for Java or trying to explicitly control the garbage collection behavior in a JVM by, for example, hanging on to objects longer than their natural lifespans is a sure way of shooting yourself in the foot. There are, however, some mechanisms in the Java language that make it possible to help the GC by giving it "hints". Some of the available mechanisms are good and some are bad. Some should be used with caution to avoid unwanted side effects.
In Java, since Java 1.0, every object contains a method called finalize
that may be freely overridden by any implementer. The contract is that the finalize
method is called just before the object in question is about to be garbage collected. This might seem like a good idea, making it possible to do cleanups before the object goes away, such as closing any open file handles that the object...