3 ms·
The author states: >"The type system in Rust is obviously very influenced by the type system of Haskell, but they also implemented “ownership” which allows for
by bogomipz 6y ago
The author states:
>"The type system in Rust is obviously very influenced by the type system of Haskell, but they also implemented “ownership” which allows for the killer feature garbage-collection free automatic memory management."
I always assumed that garbage collection was simply a convenience offered by a particular language.
I was also under the impression that memory leaks were the class of problems that were addressed by garbage collection and that memory safety was a class of problems that was concerned with things like buffer overflows and corruption and addressed by runtime checks.
However after reading I feel like I have failed to understand the role of garbage collection in enforcing memory safety. Is memory management and memory safety not so cleanly divided?
Could someone explain or point me to a good resource for understanding how garbage collection is used to enforce memory safety?
- steveklabnik 6y agoYou can think of it as kind of the inverse of leaks; if fixing a leak is "hey, this isn't used anymore, let's clean it up," fixing memory safety is "hey, this is still used, don't free it yet!" Does that make sense?
- bogomipz 6y agoYes both of these responses were helpful. Cheers.
- Jtsummers 6y agoThe common memory-related bugs are: - use after free - memory leaks (failing to free) - concurrent and uncontrolled access from multiple threads (race conditions) Garbage collection helps with the first two. The system will not free memory until it is certain (by whatever method is being used) that the memory is no longer in use. But this comes with a run-time cost. Rust's innovation here is a more reliable way of tracking memory usage statically, and removes some of the run-time cost. Paired with its ownership/borrow mechanisms it also works to solve the third issue, which GC doesn't deal with at all.
- adchari 6y agoGarbage collection is also limited, you can still effectively create “memory leaks” if you hold a reference to something you’re not longer using.