On Monday, 21 September 2015 at 09:58:31 UTC, Chris wrote:
I sometimes wonder - and please forgive me my ignorance, because I'm not a GC expert at all - if it would be possible to create a system where the created objects know their own life spans and destroy themselves, once they are no longer used. Like the cells in our bodies.
Yes, this is the system Rust uses, but the compiler has to prove the life spans at compile time. So how convenient that is, is limited to the capabilities of the prover and for how long you want to wait for the computation of the life times.
In essence you have to choose between a simple tracking system that is a bit annoying and solving NP-complete problems (which may work out fine in many cases, but not in all possible configurations).
There are many ways to improve on this based on what tradeoffs you accept. Like you could segment the heap and prove that at a given point all the objects in the heap have to be dead, and just accept that you waste some memory up until that point.