A garbage collector automatically frees up memory that a program isn't using anymore.
For example, say we did this in Python :
Look at nums_sorted in get_min. We allocate that whole list inside our function, and once the function returns we don't need the list anymore. In fact, once the function returns we don't have any references to it anymore!
What happens to that list in memory? The Python garbage collector will notice we don't need it anymore and free up that space.
How does the garbage collector know when something can be freed?
One option is to start by figuring out what we can't free. For example, we definitely can't free local variables that we're going to need later on. And, if we have a list, then we also shouldn't free any of the list's elements.
This is the main intuition behind one garbage collector strategy:
- Carefully figure out what things in memory we might still be using or need later on.
- Free everything else.
This strategy is often called Tracing Garbage Collection, since we usually implement the first step by tracing references from one object (say, the list) to the next (an element within the list).
A different option is to have each object keep track of the number of things that reference it—like a variable holding the location of an array or multiple edges pointing to the same node in a graph. We'll call this number an object's reference count.
In this case, a garbage collector can free anything with a reference count of zero.
This strategy is called Reference Counting, since we are counting the number of times each object is referenced.
In languages with reference counting, make sure you know who is responsible for maintaining the reference counts for your objects. Some languages, like C++, take care of incrementing and decrementing the count for you automatically, but others, like Objective-C, force you to manually update it.
Some languages, like C, don't have a garbage collector. So we need to manually free up any memory we're not using anymore:
We sometimes call this manual memory management.
Some languages, like C++, have both manual and automatic memory management.
Pass Your Interviews with My FREE 7-Day Crash Course
I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.
No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe if you hate it.