dark mode light mode Search Menu

Take Out the Garbage

David Villa on Flickr

Computer trash is in the form of memory. All software runs by reserving blocks of space in the computer memory to run. For example, in a script one or more values might be stored in a variable which serves as a bookmark and placeholder. When the script completes, the memory used to store the variable is no longer needed. It's garbage unless or until the data is needed.

High level languages like Python, Rails, Clojure, and PHP automatically include code to take out the trash by releasing computer memory once reserved by the software but no longer used. Low level languages like C either include garbage collection automatically or can do so with the addition of a library of code designed to perform cleanup tasks.

The most interesting part of this invisible behind the scenes trash collection is how garbage is identified. If you're a programming language, and you have 100 chunks of used memory in front of you, how would you tell which chunks need to be available and which can be deleted?

Take a moment to think about a solution. Can you figure out how to solve this problem?

The most direct solution would be to look for references. Compile a list of all software running on the computer and their references to memory with another list of 100 chunks of memory. Orphan memory chunks, those not referenced by software running on the computer, would be candidates for being deleted as trash.

In our world, you can smell trash in some cases. Other times, clothes strewn on the floor tell us the clothing is not currently needed and, therefore, should be cleaned up. Computers require a process of listing and comparing to identify their trash.

What is important about taking out the garbage in a software application? Memory management is a critical issue on computers because they have finite resources.

If you write efficient code, you reduce the memory requirements of your software. Coders also can help by releasing resources after use in their code. For example, in some cases, explicitly destroying variables that hold data is helpful. But much of the trash collection happens deep in the programming language itself as software runs and when software closes down.

It’s also important to learn what types of functionality triggers garbage collection. For example, if you build a very active software application, you want to avoid unnecessary memory cleanup as much as possible. You want to limit garbage cleanup to specific times, for example, when the application has a quiet period or there is a value to releasing memory.

So, yes, software and computers accumulate garbage, just like you and me. Except they don't mutter or complain or avoid cleanup. Worst case, computers simply bog down when their memory becomes full and they shut down. Worst case, we have to clean our rooms. Or move, which might be an option for some people.

Learn More

Definition of Memory Management


Definition of Memory Leak


Definition of Garbage Collection


Is Memory Management in Programming Becoming an Irrelevant Concern?


The Garbage Collection Page (Richard Jones)


GC-LIST Garbage Collection FAQ