3 ms·
There is a general problem with combining destructors and automatic collection of reference cycles, and its one that several languages have unexpectedly found t
by garethrees 11y ago
There is a general problem with combining destructors and automatic collection of reference cycles, and its one that several languages have unexpectedly found themselves facing.
For example, Java has the function System.runFinalizersOnExit, but if you look at the documentation you'll see that it now says, "Deprecated. This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock." http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#runFinalizersOnExit(boolean) http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.h...
The problem is discussed in detail in Hans Boehm's 2002 technical report “Destructors, Finalizers, and Synchronization”.
http://www.hpl.hp.com/techreports/2002/HPL-2002-335.pdf http://www.hpl.hp.com/techreports/2002/HPL-2002-335.pdf
Boehm's key points are: (1) When a destructor on an object O runs normally, anything that O points to is still alive (because of the reference from O), but when a cycle is collected, not everyone can go first: that is, all but one destructors in the cycle have to run after one or more of their references are dead. It is hard to write destructor code that is safe in all cases. (2) Any destructor that needs to update a concurrently accessed data structure has to take a lock, but destructors on cycles run asynchronously with respect to the rest of the program and so an unlucky timing leads to deadlock.
If this problem was better appreciated then language designers wouldn't go down the rabbit hole of trying to figure out how to make destructors work together with automatic collection of reference cycles, and instead try the alternative approach of providing mechanisms for the program to run destructors synchronously.
In the Memory Pool System we use a message-passing interface: http://www.ravenbrook.com/project/mps/master/manual/html/topic/finalization.html http://www.ravenbrook.com/project/mps/master/manual/html/top...