GC (Garbage Collection)
Garbage Collection
A mechanism that automatically cleans up memory a program no longer uses so it can be reused.
In Simple Terms
Garbage collection is a feature that automatically finds and frees memory a program is no longer using while it runs. While an app on a smartphone or similar device is running, all kinds of data get stored in memory, and if unneeded data is left behind, running low on memory can cause the app to misbehave. By reclaiming unneeded data in the background, this feature makes it easier to keep free space available for other tasks.
Behind the Name
Garbage Collection literally means "collecting trash." It's named for the way it treats data that's no longer needed within memory a program was temporarily using as "trash," automatically gathering and clearing it up.
Take a Closer Look!
Garbage collection (GC) is a mechanism that automatically detects and frees portions of memory a program temporarily allocated while running once they're no longer referenced and therefore unneeded.
Put simply, it acts like a "trash collector" inside a computer.
Memory capacity is limited, so if unused data is left in place, there's eventually no room for new data, which can cause the program to stall.
There's more than one way to manage memory in languages without GC. In C, for example, developers write code themselves to explicitly return each block of memory once it's no longer needed, while in Rust the language automatically returns memory as soon as a variable's role ends.
With the former approach, forgetting that instruction easily causes a "memory leak," where available memory keeps shrinking.
GC became widely adopted as a way to watch actual memory usage during execution, judge that certain data is no longer reachable from anywhere, and reclaim it as trash.
Specifically, the system targets data no longer traceable from any variable and dynamically reclaims it, for instance when free memory runs low for placing new data.
This frees developers from fine-grained memory management so they can focus on building the program itself.
However, since collection can briefly pause the program for an instant, extra care is sometimes needed in cases like games that demand very high speed.