Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

My (least) favourite reason for avoiding Object.finalize is not that objects might get finalized after you expect it, but they can get finalized before you might expect it. The problem is not that an object that is still in scope can be finalized before the scope is exited if Java decides it is no-longer reachable.

void test() { HasFinalize myObject = ...; OutputStream os = myObject.stream; // myObject is no-longer reachable at this point, // even though it is in scope. But objects are finalized // based on reachability. // And since finalization is on another thread, it // could happen before or in the middle of the write .. // closing the stream and causing much fun. os.write("Hello World"); } 

See this questionthis question for more details. Even more fun is that this decision might only be made after hot-spot optimisation kicks in, making this painful to debug.

My (least) favourite reason for avoiding Object.finalize is not that objects might get finalized after you expect it, but they can get finalized before you might expect it. The problem is not that an object that is still in scope can be finalized before the scope is exited if Java decides it is no-longer reachable.

void test() { HasFinalize myObject = ...; OutputStream os = myObject.stream; // myObject is no-longer reachable at this point, // even though it is in scope. But objects are finalized // based on reachability. // And since finalization is on another thread, it // could happen before or in the middle of the write .. // closing the stream and causing much fun. os.write("Hello World"); } 

See this question for more details. Even more fun is that this decision might only be made after hot-spot optimisation kicks in, making this painful to debug.

My (least) favourite reason for avoiding Object.finalize is not that objects might get finalized after you expect it, but they can get finalized before you might expect it. The problem is not that an object that is still in scope can be finalized before the scope is exited if Java decides it is no-longer reachable.

void test() { HasFinalize myObject = ...; OutputStream os = myObject.stream; // myObject is no-longer reachable at this point, // even though it is in scope. But objects are finalized // based on reachability. // And since finalization is on another thread, it // could happen before or in the middle of the write .. // closing the stream and causing much fun. os.write("Hello World"); } 

See this question for more details. Even more fun is that this decision might only be made after hot-spot optimisation kicks in, making this painful to debug.

Source Link

My (least) favourite reason for avoiding Object.finalize is not that objects might get finalized after you expect it, but they can get finalized before you might expect it. The problem is not that an object that is still in scope can be finalized before the scope is exited if Java decides it is no-longer reachable.

void test() { HasFinalize myObject = ...; OutputStream os = myObject.stream; // myObject is no-longer reachable at this point, // even though it is in scope. But objects are finalized // based on reachability. // And since finalization is on another thread, it // could happen before or in the middle of the write .. // closing the stream and causing much fun. os.write("Hello World"); } 

See this question for more details. Even more fun is that this decision might only be made after hot-spot optimisation kicks in, making this painful to debug.