Skip to content
BENIBIX Learn

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.

Development⚙️ Compilers & Runtimes

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.

Example
The team that makes the Chromium web browser looked at its worst security bugs. About 70% of them came from unsafe use of memory.
Why it matters
Memory bugs cause a lot of the worst security holes. A safe language rules out whole kinds of them.
Full entry
Development⚙️ Compilers & Runtimes

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.

Example
Lucy's JavaScript function builds a big list, uses it and returns. Nothing points at the list now. The garbage collector frees it a moment later.
Why it matters
It gets rid of a whole kind of bug, like freeing the same memory twice. So most big languages have it.
Full entry

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.