Computer Memory: RAM, ROM, and Cache
Understand how primary memory works and the difference between volatile and non-volatile storage.
Knowledge of Computers › Fundamentals of computer sciences · 18 min read
In the previous chapter, we explored the different types of computers, from the smartphone in your pocket to massive supercomputers predicting the weather. But regardless of how big or small a computer is, it cannot do anything without a place to hold its thoughts. It needs memory.
If the Central Processing Unit (CPU) is the brain of the computer, memory is its workspace. In this chapter, we are going to dive deep into the internal memory of a computer—specifically Primary Memory. We will break down the differences between RAM and ROM, explore their various sub-types, and understand why Cache memory is the secret weapon for a fast computer.
(Note: We will save hard drives, SSDs, and memory measurement units like Megabytes and Gigabytes for the next chapter on Secondary Storage. Here, we are focusing strictly on the memory that lives right next to the CPU.)
The Office Desk Analogy: How Memory Works
To understand computer memory, you don't need a degree in electronics. You just need to imagine an office worker sitting at a desk.
Imagine you are that worker (the CPU). You are incredibly fast at reading documents, doing math, and making decisions. But to do your job, you need a workspace and a place to keep your files.
- 1Your Pockets (Cache Memory): You keep a small notepad in your shirt pocket. It only holds a few crucial numbers, but you can reach it instantly without even moving your arms.
- 2The Desk Surface (RAM): When you are actively working on a project, you pull files out and spread them across your desk. You can reach any file on the desk very quickly. However, the desk has limited space. When you go home at night, the cleaning staff clears the desk completely.
- 3The Instruction Manual (ROM): Glued to the corner of your desk is a permanent instruction manual. It tells you how to turn on the lights, how to unlock your drawers, and how to start your day. You can read it, but you cannot erase or rewrite it.
- 4The Filing Cabinet (Secondary Storage): In the next room, there is a massive steel filing cabinet. It holds thousands of files permanently. But to get a file, you have to stand up, walk to the next room, find the folder, and bring it back to your desk. It is huge, but it is very slow.
In this chapter, we are focusing entirely on the notepad, the desk surface, and the instruction manual.
The Great Divide: Volatile vs. Non-Volatile Memory
Before we look at specific chips, we must understand the most important dividing line in computer memory: what happens when the power goes out?
Think of volatile memory like drawing on a whiteboard—easy to write, easy to erase, but if someone wipes it (power loss), it's gone. Non-volatile memory is like carving words into stone—it stays there whether the lights are on or off.
RAM: Random Access Memory (The Desk Surface)
RAM is the main working memory of the computer. When you open Microsoft Word, play a video game, or load a web page, the computer copies the necessary files from your slow filing cabinet (the hard drive) and places them onto the fast desk surface (RAM).
Why is it called "Random Access"? In older technologies, like magnetic cassette tapes, memory was sequential. If you wanted to listen to the 5th song on a tape, you had to fast-forward through songs 1, 2, 3, and 4. RAM is different. The CPU can jump directly to any specific location in the memory instantly, in any random order, without having to read through the data before it. Accessing the first piece of data takes the exact same amount of time as accessing the millionth piece of data.
RAM is volatile. This is why, if you are typing a long document and the power goes out before you hit "Save" (which copies it to the non-volatile hard drive), you lose your work. The desk was wiped clean.
Types of RAM: SRAM vs. DRAM
Not all RAM is built the same. Engineers have developed two main types of RAM, each serving a different purpose based on speed and cost.
1. DRAM (Dynamic RAM) DRAM is the most common type of main memory in computers. When you buy a laptop that says it has "8GB of RAM," it is talking about DRAM.
- How it works: DRAM stores each bit of data (a 1 or a 0) using a microscopic transistor and a capacitor. Think of a capacitor like a tiny bucket holding electrical charge. A full bucket is a "1", an empty bucket is a "0".
- The Catch: These tiny buckets leak. Very quickly, the electrical charge drains away. To prevent the computer from forgetting the data, the system must constantly read the data and rewrite it, refilling the buckets thousands of times per second. This process is called refreshing.
- Pros and Cons: Because it only uses one transistor and one capacitor per bit, DRAM is very cheap to manufacture and can pack a massive amount of storage into a small chip. However, the constant need to "refresh" makes it relatively slow.
2. SRAM (Static RAM) SRAM is the high-performance sports car of the RAM world.
- How it works: Instead of leaky buckets, SRAM uses a complex circuit of four to six transistors (called a flip-flop) to hold a single bit of data. Once a 1 or a 0 is locked into this circuit, it stays there perfectly.
- The Catch: It does not need to be refreshed, which makes it incredibly fast. However, because it requires up to six transistors just to hold one bit of data, it takes up a lot more physical space on a microchip and is very expensive to manufacture.
- Pros and Cons: It is blazing fast, but because of the cost and size, computers only use tiny amounts of SRAM. It is primarily used for Cache memory (which we will cover shortly).
ROM: Read-Only Memory (The Instruction Manual)
If RAM is wiped clean every time the computer turns off, how does the computer know how to turn back on? How does it know it has a keyboard attached, or where to find the operating system?
This is where ROM comes in. ROM is non-volatile. The data is written onto the ROM chip during the manufacturing process (or programmed later using special tools) and, generally speaking, the computer can only read from it. It cannot easily write new data to it during normal operations.
The Role of ROM: Booting Up
When you press the power button on your computer, the CPU wakes up with a completely empty RAM. It has no idea what to do. It is hardwired to immediately look at the ROM chip.
The ROM chip contains a small piece of software called the BIOS (Basic Input/Output System). The BIOS performs a POST (Power-On Self Test) to check if the RAM, keyboard, and hard drive are functioning. Once the check is complete, the BIOS finds your operating system (like Windows) on the hard drive and loads it into the RAM. Without ROM, your computer would just be a dead box of metal.
The Evolution of ROM Types
Over the decades, ROM has evolved. Originally, it was strictly "read-only," but engineers eventually found ways to make it slightly more flexible.
- MROM (Masked ROM): The oldest type. The data was physically hardwired into the silicon chip at the factory using photographic masks. It was completely permanent. If the manufacturer made a typo in the code, they had to throw the whole batch of chips in the trash.
- PROM (Programmable ROM): To solve the waste issue of MROM, PROM was invented. These chips are manufactured completely blank. A software developer can buy them and use a special machine (a PROM programmer) to burn data onto the chip. However, this is a one-time process. Tiny internal fuses are permanently blown during programming. Once written, it can never be changed.
- EPROM (Erasable Programmable ROM): Engineers wanted a chip they could reuse. EPROM chips have a tiny, transparent quartz window on the top of the chip. If you peel back the protective sticker and expose that window to strong Ultraviolet (UV) light for about 20 minutes, it wipes the chip completely clean, allowing it to be reprogrammed.
- EEPROM (Electrically Erasable Programmable ROM): Shining UV light on a chip was highly inconvenient. EEPROM solved this. It can be erased and reprogrammed using normal electrical signals right inside the computer, without needing to remove the chip. You can erase specific bytes of data without wiping the whole chip.
Cache Memory: The VIP Fast Lane
We have talked about the CPU (the brain) and RAM (the desk). But there is a major problem in modern computing: The CPU is too fast.
Modern CPUs can process billions of instructions per second. DRAM (your main RAM) is fast, but it is not that fast. If the CPU has to wait for DRAM to fetch data every single time, the CPU ends up sitting idle, twiddling its thumbs, waiting for the data to arrive. This waiting period is a famous bottleneck in computer science.
To solve this, engineers introduced Cache Memory (pronounced "cash").
Cache is a very small, ridiculously fast memory chip built directly into (or very close to) the CPU. It is made of the expensive SRAM we discussed earlier. Cache acts as a buffer between the super-fast CPU and the slower RAM.
How Cache Works
When the CPU needs data, it doesn't go to the RAM first. It checks the Cache.
- 1If the data is there, it's called a Cache Hit. The CPU gets the data instantly and keeps working at top speed.
- 2If the data is not there, it's called a Cache Miss. The CPU has to go to the slower RAM to get the data.
- 3Crucially, when the CPU fetches that data from RAM, it copies it into the Cache, assuming, "If I needed this data now, I will probably need it again in a millisecond."
The Levels of Cache (L1, L2, L3)
Because SRAM is so expensive and takes up so much physical space on the processor, we can't just give a computer 8GB of Cache. Instead, Cache is divided into a hierarchy of levels:
- L1 Cache (Level 1): The smallest and fastest cache. It is built directly inside the CPU core itself. It usually only holds a few Kilobytes (KB) of data, but it operates at the exact same speed as the CPU.
- L2 Cache (Level 2): Slightly larger (usually measured in Megabytes) and slightly slower than L1. It is still incredibly fast and is usually located on the CPU chip, serving as a backup to L1.
- L3 Cache (Level 3): The largest and slowest of the cache levels, but still much faster than RAM. In multi-core processors, the L3 cache is usually shared among all the different cores, acting as a communal pool of fast memory.
The Data Fetch Sequence
- 1The CPU needs a piece of data.
- 2It checks the ultra-fast L1 Cache.
- 3If not found, it checks the L2 Cache.
- 4If not found, it checks the shared L3 Cache.
- 5If not found, it reaches out to the main RAM (DRAM).
- 6If not found in RAM, it must pull it from the slow Secondary Storage (Hard Drive).
The Memory Hierarchy: Putting It All Together
To truly master computer memory, you need to understand the trade-offs engineers make. You cannot have memory that is simultaneously massive, lightning-fast, and cheap. You have to choose two.
Therefore, computers use a Memory Hierarchy—a layered approach to balance speed, size, and cost.
If we look at memory from the perspective of the CPU, moving from the closest/fastest to the furthest/slowest, the hierarchy looks like this:
- 1Registers: The absolute fastest memory. These are microscopic holding areas built directly into the CPU's processing logic. They hold the exact numbers the CPU is doing math on at this exact microsecond. (Size: Bytes).
- 2Cache Memory (SRAM): The fast waiting room for data the CPU is about to use. (Size: Kilobytes to Megabytes).
- 3Main Memory (DRAM): The active workspace for currently running programs. (Size: Gigabytes).
- 4Secondary Storage: The permanent filing cabinet (Hard Drives, SSDs). Massive, cheap, non-volatile, but very slow. (Size: Gigabytes to Terabytes).
As you move down this list from Registers to Secondary Storage:
- The Speed decreases.
- The Cost per byte decreases.
- The Storage Capacity increases.
Quick Revision
- Primary Memory is the internal memory the CPU interacts with directly, mainly consisting of RAM, ROM, and Cache.
- Volatile Memory (RAM, Cache) loses all data when power is turned off. Non-Volatile Memory (ROM) retains data without power.
- RAM (Random Access Memory) is the computer's active workspace. Any part of it can be accessed instantly.
- DRAM (Dynamic RAM) is used for main memory. It is cheaper but must be constantly electrically "refreshed" to prevent data loss.
- SRAM (Static RAM) is used for Cache. It does not need refreshing, making it much faster but more expensive.
- ROM (Read-Only Memory) holds permanent instructions, most notably the BIOS, which boots up the computer.
- EPROM is erased using Ultraviolet (UV) light, while EEPROM is erased electrically (and is the basis for modern flash drives).
- Cache Memory sits between the CPU and RAM. It stores frequently used data to prevent the ultra-fast CPU from waiting on the slower RAM.
- Memory Hierarchy Rule: The closer memory is to the CPU, the faster, smaller, and more expensive it is.
Related notes
- How a Computer Actually Works
Understand the basic block diagram of a computer, including the CPU, ALU, and Control Unit.
- The Five Generations of Computers
Trace the evolution of computers from early vacuum tubes to modern artificial intelligence.
- Types of Computers Explained
Learn the difference between microcomputers, mainframes, supercomputers, and hybrid systems.
- Secondary Storage and Memory Units
Learn how computers store data permanently and how memory sizes are measured.