3 ms·
Compared to GC the other options have several important disadvantages. Rust style memory management: - Additional complexity in the language and in your code
by jules 11y ago
Compared to GC the other options have several important disadvantages.
Rust style memory management:
- Additional complexity in the language and in your code
- You need unsafe blocks for code that does something interesting with pointers
- Can leak unreachable memory even in safe code
- Still has pauses due to deallocating large reference chains, or puts them on a deferred free list in which case you no longer have prompt deallocation
Refcounting with weak refs:
- Unsafe
- A little bit of extra work
- Can easily introduce memory leaks, especially with closures
- Refcounts have overhead in time and memory
- High contention on the refcounts in multithreaded code, or can't share data among threads at all (or need hazard pointers or RCU)
- Still has pauses due to deallocating large reference chains, or puts them on a deferred free list in which case you no longer have prompt deallocation
Most code is actually fine with GC, and some code is significantly simpler with GC, such as lock free data structures.
Moreover some of the problems with GC are not fundamental, and can be fixed:
- Pause times can be much lower, e.g. realtime GC's and Azul GC
- The main problem with languages like Java is that they
have pointers and heap allocation everywhere. If you used the same data in a refcounted language it would be horribly slow as well. In C# with support for value types this is a bit better but still not ideal.