4 ms·
> Modern languages have two patterns for more complex memory management I count four, even if you discount pure static pre-allocation: 1. Manual dynamic memor
by msla 4y ago
> Modern languages have two patterns for more complex memory management
I count four, even if you discount pure static pre-allocation:
1. Manual dynamic memory allocation and deallocation, as with malloc() and free() and, in C++, new and delete, possibly augmented with things like RAII which don't fundamentally change it.
1.5. Reference counting lives somewhere in here, between pure manual allocation/deallocation and true garbage collection, in that it automates some aspects of resource management but needs some extra manual help to deal with circular data structures that real garbage collection has no problem with.
2. Garbage collection, in multiple different algorithms and optimizations.
3. Linear types, which enable static lifetime analysis, as is done in Rust.
4. Purely declarative languages and other extremely high-level languages that effectively take it out of the programmer's hands entirely and handle everything implicitly.
- dragontamer 4y agoReference counting is Garbage Collection (and is Python's method of Garbage collection). Borrow Checker is... hmm... I guess that's a new-ish pattern. Its inspired from C++ unique_ptr and auto_ptr (now obsolete, use unique_ptr instead). I'd say that Borrow Checker is still manual memory management, except the compiler writes the free() for you. #4: purely declarative languages and such is going to be garbage collected in practice. ---------- So 1.5, 2, and 4 are all garbage collection. #3 / Rust's borrow checker is debatable. I'm still inclined to call that manual malloc/free(), even if the compiler handles the free() for you. Because the compiler can only free() if you code it in the exact way that Rust (or C++ unique_ptr) expects the memory to be used.
- msla 4y agoApparently, Python split the difference and uses reference counting alongside garbage collection: https://rushter.com/blog/python-garbage-collector/ https://rushter.com/blog/python-garbage-collector/ However: > I'd say that Borrow Checker is still manual memory management, except the compiler writes the free() for you. And: > #4: purely declarative languages and such is going to be garbage collected in practice. My point is to catalog the different ways programmers have to think about resource usage, not implementation details. Python is a GC'd language, and unlike C++ with smart pointers, because it doesn't make the programmer manually manage memory in order to avoid a leak, and regular expressions and SQL are high-level in that the languages don't even offer a way to express the concept of resource management.
- Jtsummers 4y ago> Apparently, Python split the difference and uses reference counting alongside garbage collection: Splitting the difference between what? Both reference counting and generational garbage collection algorithms are garbage collection algorithms. The generational garbage collection algorithm is to handle issues with things like cyclic references (A->B, B->A) which would cause a pure reference counting garbage collector to retain the memory even if nothing else referenced A or B.
- saghm 4y agoI tend to think of the way Rust does memory management as a compile-time equivalent to garbage collection. Instead of figuring out at runtime when to free memory, the compiler instead determines when allocations can be freed and puts in the calls to free itself. The key difference between this and manually calling malloc/free is that Rust makes use after free a compiler error instead of undefined behavior at runtime.
- steveklabnik 4y ago> Its inspired from C++ unique_ptr and auto_ptr....the compiler writes the free() for you. The borrow checker does not determine when to call free. It also doesn't call malloc. It is completely orthogonal to heap allocation.
- vlovich123 4y agoI think people in this thread say borrow checker but mean RAII where the compiler injects Drop for you automatically. But that’s not so much the borrow checker and more lexical lifetimes. It‘s possible the borrow checker is involved with non lexical lifetimes? Not sure
- steveklabnik 4y agoThe borrow checker is what checks lifetimes, that's correct. "Non-lexical lifetimes" is an example of describing certain semantics of what the borrow checker checks, yes.