beanz Magazine

Take Out the Garbage

David Villa on Flickr

In the same way your bedroom may be impossible to enter if you let dirty clothes pile up, computers can crash and refuse to operate if their memory is stuffed with unused data.

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

http://whatis.techtarget.com/definition/memory-management

Definition of Memory Leak

http://en.wikipedia.org/wiki/Memory_leak

Definition of Garbage Collection

http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29

Is Memory Management in Programming Becoming an Irrelevant Concern?

http://programmers.stackexchange.com/questions/189542/is-memory-management-in-programming-becoming-an-irrelevant-concern

The Garbage Collection Page (Richard Jones)

http://www.cs.kent.ac.uk/people/staff/rej/gc.html

GC-LIST Garbage Collection FAQ

http://www.iecc.com/gclist/GC-faq.html

Also In The December 2013 Issue

An Interview with Troy Hunt

Troy Hunt is a software architect and Microsoft Most Valued Professional (MVP) focusing on security concepts and process improvement in a Fortune 50 company. He's based in Australia.

1Password, LastPass, RoboForm

If you use a password you created that is less than eight characters, your password is vulnerable to hacking. Here are three ways to create and use secure passwords online.

How to Write Secure Code

Coding securely doesn't have to kill the joy of programming. In fact, learning how to code securely provides insights into languages and computing.

How to Code HTML Email

How to code an HTML email like the ones you open every day turns out to be an offbeat software coding challenge.

What is an SSL Certificate?

How to tell if a web page is secure is one of the most basic yet least obvious ways to protect your data online.

Where to Find Command Line Interface Software

One key computing skill is the ability to use command line interface (CLI) software to enter commands to control a computer. Here are some options.

Lua

Lua is a comparatively simple programming language used in a wide range of places, from digital TVs to video games to phone applications. It's also designed to be simple to use and lightweight.

Arrays

Here is how three programming languages handle a common problem: how do you organize and keep track of useful data?

Linux Command List for Command Line Interfaces

Some of the most common commands you'll need for a command line interface (CLI), in a Linux command list.

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.

News Wire Stories for October 2013

Must read stories about computer science, software programming, and technology for September 2013.

Learn More Links for October 2013

Links from the bottom of all the October 2013 articles, collected in one place for you to print, share, or bookmark.