A Quick & Dirty Guide to Computer Memory
Just the basics
Even I, as a distinctly average (although somewhat aspirational) computer user, have heard terms like “RAM,” “hard drive,” “gigabyte,” “bit,” “memory,” but what does it all mean?
Memory and Hard Drive: What’s the Difference?
Computer memory is the system a computer uses to represent and store data for immediate use and is primarily made up of random-access memory (RAM). RAM contains data and instructions currently in use; that data is fast to access and is lost when the computer is turned off.
That’s where your hard drive comes in.
The bulk of the computer's data (programs, files, etc.) is stored on the hard drive. Data on the hard drive persists when the computer is turned off and unplugged. Accessing this data takes more time, but the storage capacity is far greater.
What’s all this nonsense about giga- and terabytes?
Capacity. The measure of how much binary code a computer can store. Now we know a computer can store data in random-access memory and the hard drive, we can expect two values when looking at capacity specifications for a computer.
What’s binary got to do with storage capacity?
The binary number system is made up of two numbers: 0 and 1. Computers only understand electrical signals, where an OFF signal is represented by 0 and ON by 1. Binary code uses these 0s and 1s to represent data, e.g., a letter or number. Each of these 0s or 1s is called a bit.
Two bits can be represented 2², or 4, ways: 00, 01, 10, 11
Three bits can be represented 2³, or 8 ways: 000, 001, 010, 011, 100, 101, 110, or 111.
Eight bits, also known as a byte, can be represented 2⁸, or 256, ways.
As you can see, as the number of bits increases, the data that can be represented increases exponentially.
So, that's what binary has to do with the storage capacity of your computer.
Not All Memory is Created Equally
Stack vs. Heap Memory
Firstly, both stack and heap memory are part of the RAM. So, what’s the difference?
Stack memory is local memory. When a function — we’ll call it Function A — is called, a new stack frame is created for Function A. If you add variables to Function A, they are stored in its stack frame. When a new function — Function B — is called, the stack frame for Function A is saved in stack memory, and a new stack frame is created for Function B. When Function B is completed, its stack frame is removed from memory, and the same occurs for Function A following its completion.
NOTE: stack memory is limited. Have you ever accidentally written an infinite loop, and the error you received was something along the lines of “maximum call stack size exceeded”? Now you know a little bit more about what’s going on!
Heap memory is dynamic memory. It is allocated when a function has been called. Heap memory stores larger non-primitive data structures such as dictionaries and lists. The variable referencing the data structure remains in stack memory and points to the object itself, stored in heap memory. When the program has finished executing, the heap and stack memory is cleared as described above.
NOTE: some languages, like C, require you to allocate and deallocate memory manually, while others, like JavaScript or Python, take care of it for you.