beanz Magazine

Introduction to Memory Management

John Loo on Flickr

Dive into the nuts & bolts of storing pictures and files on your computer.

Ever wondered what happens behind the scenes when you hit the “run” button and your code springs to life? To develop a deeper understanding of computing, let’s take a look at an essential, if somewhat mysterious process: storing programs in memory.

BREAKING DOWN INTO BINARY

Every variable needs to saved somewhere. Otherwise, your program will forget values when it moves to the next line of code!

At a base level computers work with 0s and 1s. So before we store our data, we must transform numbers, letters, and complex audio-visual objects into simple 0s and 1s.

Small integers (whole numbers) like “5” or “37” are stored as binary versions of themselves. “5” becomes “0101” and “37” is “100101”. For negative numbers, we use a clever system called “two’s complement”. First, you take the binary form of the number’s positive equivalent, then you flip each bit and add one at the end. For example, “5” is flipped from “0101” to “1010”, and then finishes as “1011”. This representation makes it easy to add or subtract integers.

To keep things simple, most operating systems store integers in standardized 32-bit blocks. Even tiny integers that only need 1 or 2 bits of space!

Letters are stored using ASCII (American Standard Code for Information Interchange). ASCII is a not-so-secret code where every number from 0-265 corresponds to a particular letter or symbol. Capital “A”, for example, is 65. A question mark is 63, and the letter “1” becomes the number 49. ASCII codes are then stored using the same rules as integers. The string “Hello world” has 10 letters and 1 space, which requires 11 ASCII characters and therefore 11 bytes (88 bits) of space.

As you can see, different types of data require different amounts of memory storage. In “typed” languages like Java or C, you specify the size and type of your variables when you write the code, which makes things simple for the compiler, but requires you to know the size of all your data in advance.

STORING VARIABLES

Let’s imagine values and variables as cats in boxes. The cat represents the actual value, like “5” or “Hello world”. The variable is just a convenient box where a cat can sit until it deigns to leave. During the runtime of a program, a single box might hold hundreds of different cats. This system is also a convenient way for the program to find a cat very quickly — just look inside the value’s box!

In real life, each “box” is a tiny spec on a memory module deep inside the guts of your laptop (or phone, tablet, or hard drive). Each bit of data is stored as a 0 or 1 inside a special electronic circuit. With infinite patience and a very powerful microscope, you could crack open your laptop and locate the exact physical space where your variable is stored. In practical terms, this is impossible, because there are billions and billions of circuits inside each memory module!

Each variable “box” has an address, the same way your house has an address. When a program is booted up, the operating system hands it a chunk of memory with a series of addresses it can use to store instructions and variables. All memory addresses are hexadecimal numbers. So instead of looking for “123 Rainbow Rd” your program might search for “#0000 0340” or “#AF83 1E82” to find a particular integer or string.

STACK AND HEAP

Depending on where you declare your variable, it’s either going to end up with an address on the stack or on the heap.

Local variables — things declared inside functions — get put on the “function call stack” and stored together. So if you declare three integers in a row, they’ll probably be neighbours with continuous addresses! This makes it easy for the program to find and manipulate data inside functions.

The heap is for “dynamically allocated memory”. You don’t always know how much space your program needs, especially when you’re reading user input or manipulating big graphics files. In “low-level” languages, like C and C++, you can write code to ask the OS for a chunk of memory during the runtime of your program. The OS responds to your request with a hexadecimal value: the address of a new empty box to store your data! This value is also referred to as a pointer because it’s not a real piece of data, it’s just sign post for a storage area.

Practically speaking, the only difference between the stack and the heap is when and how the program stores variables inside them. They’re both just chunks of memory: electronic circuits used to store bits. Separating the two helps programmers stay organized, the same way you separate spoons and forks inside your cutlery cabinet to make things easier to find.

All of these actions happen inside a computer’s RAM, also known as “random-access memory”. RAM is “live” memory that requires electricity to keep values stored. If the power goes out — poof! The data is lost. For this reason RAM is also called “volatile” memory. On the plus side, it’s fast and easy to access.

CONCLUSION

To review what we’ve learned:

  • All variables are stored as bits in different formats.
  • Programs locate variables using their hexadecimal addresses.
  • Each address corresponds to a physical circuit inside your computer’s memory module.
  • Inside a program, memory is separated between the stack and the heap. Each one stores different kinds of variables.
  • Programs are loaded inside RAM, which requires electricity to stay running.

Managing memory is a tricky business. People who work in computer security or embedded systems deal with the nuts and bolts of storing bits and optimizing function calls. Improper memory management can cause “memory leaks”, which makes programs slow and buggy or become a gateway for hackers!

For high-level programmers, it’s important to remember that things we do all the time — like writing code using words instead of bits, or importing graphics into our programs — are abstractions. There’s a whole world of details and processes happening behind even the simplest line!

Learn More

Crash Course Videos on YouTube

Registers and RAM
https://www.youtube.com/watch?v=fpnE6UAfbtU
Memory & Storage
https://www.youtube.com/watch?v=TQCr9RV7twk

Binary Numbers

https://www.kidscodecs.com/binary-numbers/
https://www.ducksters.com/kidsmath/binary_numbers_basics.php

ASCII

https://www.kidscodecs.com/ascii/

Variables, Constants, and Data Types

https://www.kidscodecs.com/variables-constants-data-types/

What is an Operating System?

https://www.kidscodecs.com/what-is-an-operating-system/

Stack vs Heap Memory in C++ Video

https://www.youtube.com/watch?v=wJ1L2nSIV1s

Also In The December 2018 Issue

Create colourful geometric patterns in SketchUp inspired by the Great Mosque of Cordoba.

Learn how to use animated GIFs to create proper animation loops in Scratch.

Create an electronic obstacle course and test the steadiness of your hands as you navigate through.

What’s allowed in an e-mail address? Let’s break down the syntax.

Throwing your old tech in the trash is bad for the environment. Check out some safer, cleaner alternatives.

New, crafty mods that’ll push your Minecraft design skills to new levels.

Part philosophical paradox, part coding puzzle, quines are sure to tease and challenge your brain!

5 questions with the designer pushing innovation in 3D printed clothes.

All work and no play makes it harder for kids to learn, especially those with anxiety and learning disabilities.

Dive into the nuts & bolts of storing pictures and files on your computer.

A clever AI is creating twisted, surreal art. Math or magic?

Two doors hide a zonk, and one door hides a prize. Can probability & statistics help you pick the right one?

How can you tell if a credit card number is valid or invalid? Meet the Luhn algorithm, one of many checksums helping us keep the internet in order.

Learn about the Destination Dispatch algorithm and create your own robotic lift.

Grab your friends & disconnect from the digital world with these fun science & tech board games.

Interesting stories about computer science, software programming, and technology for December 2018.

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