Memory Safety vs Garbage Collection
A garbage collector frees memory you are done with. Memory safety means code can never reach memory that is not its own. Rust is safe without one.
Memory Safety
A guard that stops code from using memory it does not own.
Memory safety means a program cannot read or write memory it should not touch. In C and C++, code can write past the end of an array. It can also use memory after it was freed. That can crash the program or let an attacker take over. Rust, Go, Java, C# and Python check for or block these mistakes.
Garbage Collection
The language finds memory no one uses now. Then it frees it.
Garbage collection is when the language frees memory once nothing uses it. A part of the runtime, the garbage collector, looks for data that no part of the program can reach. It then gives that memory back so it can be used again. Java, C#, Go, JavaScript and Python all work this way. The cost is extra work in the background, and sometimes short pauses.
In one line
- Memory Safety
- A guard that stops code from using memory it does not own.
- Garbage Collection
- The language finds memory no one uses now. Then it frees it.