The most important automation in programming happened 68 years ago.
It's 1959. John McCarthy is building Lisp.

He has a problem.
Programmers keep forgetting to free memory.

For those who don’t know, in languages like C, you have to manage memory manually.
For example, if you want to use a variable, you have to allocate memory for it:
// 1. Allocate memory
int *p = (int *)malloc(sizeof(int));
*p = 42;A space in the RAM is then reserved for the variable as long as the program is running.

Now, what happens if you keep allocating memory?
Memory runs out because RAM is limited.
This situation is called a memory leak.
To prevent that from happening, you have to release the memory you no longer use:
// 2. Free the memory
free(p);Apparently, humans aren’t very good at that.
Like, the average programmer looks like this:

What happens is that programs crash. Computers run out of RAM. Everyone's in hell.
John McCarthy says it can’t happen to the language he creates. He will save everyone.

So he invents something radical. Something that cleans memory automatically.
Garbage collection.

Here's how it works.
Your program creates two variables, X and Y.
Both point to some memory.

Now you set Y to null because you don’t need it. Y's memory has no pointer anymore.

Normally, it’s time to release it manually. But now you have a garbage collector.
It runs automatically when the system doesn’t have enough memory.
First, the garbage collector marks everything reachable from all your variables.
X's memory? Marked.
Y's memory? Not marked.

Then it sweeps.

Everything not marked gets deleted.

Memory is freed automatically.
No malloc. No free. No memory leaks.
This 1950s invention runs every popular programming language today.
Including Python, JavaScript, and Java.
That’s why you mostly don’t have to worry about memory management in these languages.

Fee from Anime Coders
