Definition
Garbage collection is an automatic cleanup system that finds memory a running program no longer uses and frees it up. Developers don't have to hand back memory one piece at a time. The computer makes room by itself.
What if you keep pulling things out and never put them away?
Think about studying at a desk. You keep taking out new textbooks and notebooks, but you never put the finished ones back. Soon the desk is full, and there's no room for a new book.
Computer programs work the same way. Every time a program does something, it loads data into a workspace called main memory (RAM). If it never clears the data it has finished with, the computer's memory fills up. This is called a memory leak.
In languages without garbage collection, programmers must write instructions in their code by hand, such as "I'm done with this data, delete it." If they forget one, or delete the wrong data by mistake, the result is often a sudden program crash.
How does it tell what's garbage?
A garbage collector (the program that plays the role of the robot vacuum) regularly scans memory to find things to clean up. In the most common approach, the rule for calling something garbage is very simple: "Can I still reach it right now?" Some systems count how many links point to each piece of data and delete it when the count hits 0.
The collector starts from the program's running code and follows the arrows to every piece of data connected to it. The technical term for this is reachability. If some data is cut off and no path from the running code leads to it, nobody can ever use it again. So it's judged to be "garbage" and removed.
For example, when you defeat a monster in a game, its data disappears from the screen and is no longer connected to anything. The collector quietly deletes that disconnected data and hands the space back as room for new data.
A bit more precisely: cleaning isn't free
Thanks to garbage collection, programmers are freed from the headache of clearing memory by hand. Many programming languages, such as Java, Python, and JavaScript, include it by default.
But even this seemingly perfect cleaning robot has a cost. While it searches the whole room for garbage, the program often has to pause what it's doing. In computing this is called stop-the-world. These days, many collectors do most of their cleaning at the same time as the program runs, which makes the pause very short.
It's only a split-second pause. But in a high-end 3D game or a stock trading system, where even 0.001 seconds matters, the screen can stutter or lag. That's why systems that need top performance and instant response (such as those written in C or C++) still sometimes have people manage memory by hand.
Common misconceptions
If you use a language with garbage collection, you will never run out of memory.
If a program accidentally keeps holding on to data it has finished with, the collector can't tell it's garbage and can't delete it. This is called a "logical memory leak."
Where you meet it
When you delete a list item or a popup on a web page, the JavaScript engine's garbage collector cleans up the data that is no longer used.
When you move to another screen in a smartphone app, the memory holding the previous screen's images is freed automatically.
In one sentence
Garbage collection is a handy automatic system that finds data in memory the program no longer uses and cleans it up.