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

Siblings Pete and Alexa Ingram-Cauchi Talk iD Tech and Tech Summer Camps

They talk about how they started and run iDTech summer camps together and how parents can evaluate tech summer camps.

Where to Recycle Electronics

Here are a few places where you can recycle your old electronics safely.

What is a High Level Language?

What are the differences between high level languages and machine languages? And how do these differences impact coding?

An Interview with Boone Gorges

Learn how a humanities PhD became a software programmer who builds online communities for universities, as well as Lead Developer for BuddyPress and helping to create WordPress plugins like Anthologize and Participad.

How to Make (and Keep) New Years Resolutions

A few great ideas on how to make New Year's resolution you might actually keep, and have fun doing so. Whether you like structure or hate it, here are a few approaches and a number of resources to help.

News Wire Stories for December/January

Interesting stories about computer science, software programming, and technology for the month of November 2013.

No computer has ever been designed that is ever aware of what it’s doing; but most of the time, we aren’t either.

The Hungry Camel

How many measures of grain can one camel eat while delivering grain, before the camel runs out of grain to deliver? A fun math problem at least 1,000 years old.

How to Do Online Research

Online research skills are critical for software programmers. It's how you learn any language, by searching for error messages and looking up reference material.

Comments

Almost all programming languages include the ability to add comments and other notes in your code. Here's how several languages work with comments.

Take Out the Garbage

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.

bin, boot, opt, and Linux File System Hierarchy Mysteries

The Linux directory structure looks confusing compared to Windows. Here are the names and purpose of each directory.

What is Localhost?

Localhost is available on most computers, usually to display web pages. It's also useful to use to learn coding on your computer.

The Paywall and Adding Voices to Help Kids Code

With this issue, you will find some articles require subscription. Here's an explanation and how you can help add writers and voices to future issues of this magazine.

Learn More Links for December 2013/January 2014

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