DEV Community

Discussion on: Garbage collection - explain it like im 5 request (language agnostic)

Collapse
 
anpos231 profile image
anpos231

Imagine your computer memory is a collection of storage units.

Your operating system is the owner of these storage units.
Your computer program is a customer that will rent these units.

Your program will store all kinds of things there: buttons, images, text, etc...
Whenever your program needs more store space, it asks your operating system to provide it with a key to a free unit.
Your operating system will find a free unit and provide your program with a key to access it.

Your operating system cannot automatically take these keys from your program.
Your program must manage all these keys by itself, it must return them when it no longer needs it.
This is cumbersome for programmers because it means that we need to keep track all these keys (and there can be a lot of them).

The solution to the key problem is Garbage Collector (GC).
Garbage Collector is your program's janitor.
It regularly inspects all rented storage units to check if they are in use.
If Garbage Collector finds a unit that is not in use, it will automatically return the key to the owner (operating system).

This is happening automatically, without programmers having to even think about it.