http://blog.csdn.net/shanghui815/article/details/6787855html
http://www.oracle.com/technetwork/articles/javase/finalization-137655.htmljava
http://www.javaworld.com/article/2076697/core-java/object-finalization-and-cleanup.htmloracle
關於使用finalize()方法要注意的事項:ui
You don't know when objects will be finalized.this
You should imprint this important fact on your brain and forever allow it to inform your Java object designs.spa
Don't rely on finalizers to release non-memory resources 對於打開的文件句柄,須要手動去釋放。.net
An example of an object that breaks this rule is one that opens a file in its constructor and closes the file in its finalize()
method. Although this design seems neat, tidy, and symmetrical, it potentially creates an insidious bug. A Java program generally will have only a finite number of file handles at its disposal. When all those handles are in use, the program won't be able to open any more files.code
A Java program that makes use of such an object (one that opens a file in its constructor and closes it in its finalizer) may work fine on some JVM implementations. On such implementations, finalization would occur often enough to keep a sufficient number of file handles available at all times. But the same program may fail on a different JVM whose garbage collector doesn't finalize often enough to keep the program from running out of file handles. Or, what's even more insidious, the program may work on all JVM implementations now but fail in a mission-critical situation a few years (and release cycles) down the road.orm
protected void finalize() throws Throwable { if (logFileOpen) { try { closeLogFile(); } finally { super.finalize(); } } }
這是最經常使用的方法:就是保險防止會忘記。htm