Computer Memory — Study Notes
What to learn in Computer Memory
This topic is computer memory the way the one-paper exams ask it: not a chapter to read once, but a small set of facts that return paper after paper — the order of the memory hierarchy, the units from a bit to a terabyte, RAM against ROM, the ROM family, cache, and virtual memory. There are 166 practice questions on this page: 124 from the bank — 120 filed under Computer Memory and 4 retagged from other categories because they are memory questions (the order of memory speed, the registers, the Program Counter, and the full form of BIOS) — and 42 new ones written to fill the gaps the bank leaves, most of them in the places the bank never reaches: the registers by name, the units above a terabyte, SRAM against DRAM, the whole ROM family, the cache levels and their mapping, and virtual memory. The order below is the order to learn in. Start with the hierarchy, because every later section is one level of it: registers at the top, then cache, then main memory, then secondary storage. Then the units, because every size in the later sections is stated in them. Then RAM and ROM, the two halves of main memory. Then cache, the fast buffer that makes the whole machine feel quick. Last, virtual memory, the trick that lets a computer run programs larger than its RAM. Past papers — PPSC, FPSC, BPSC, NTS, KPPSC, SPSC and the local agencies — ask the same core facts again and again: the smallest unit of data, how many bytes in a kilobyte, which memory is volatile, which memory keeps the BIOS, which memory is the fastest, what virtual memory is. Learn each fact with its number and its opposite — RAM loses its data and ROM keeps it, SRAM is fast and DRAM is cheap, a kilobyte is 1024 bytes in binary and 1000 in decimal — and the questions answer themselves. Two boundary rules for this page. First, Hardware stays where it is: the RAM and ROM basics already wired on the Hardware page (what RAM stands for, that a hard disk stores data permanently) are not repeated here as bank questions; this page goes deeper instead. Second, the operating system's side of memory management — paging, page faults and thrashing — is taught here as memory, while the Operating Systems page keeps its own questions on the same ideas; the two pages teach the same facts from different angles, and the overlap is flagged in the build report for the coordinator.
The memory hierarchy: from registers to secondary storage
Every piece of data a computer works with has to be kept somewhere, and the chain of places it can be kept — from a register inside the processor to a magnetic tape in a vault — is called the memory hierarchy. It is a ladder on which four properties always move together: the closer a memory sits to the processor, the faster, the smaller, the costlier per byte and the more volatile it is; the further away it sits, the slower, the larger, the cheaper per byte and the more permanent it becomes. At the top sit the registers: the smallest and fastest storage in the machine, inside the CPU itself, holding the single word of data the arithmetic and logic unit is working on at that instant, measured in bytes. A register holds data for a very short time while it is being used. The named registers are the ones the papers ask: the Program Counter holds the address of the next instruction to be carried out; the Accumulator holds the result of an arithmetic or logical operation; the Memory Address Register holds the address of the memory location the CPU is about to read from or write to, while the Memory Buffer Register (also called the Data Register) holds the data itself while it is being moved; the Instruction Register holds the instruction currently being carried out. Below the registers comes cache memory, a small, very fast store built from static RAM that keeps the instructions and data the processor has used most recently, so the CPU need not wait for the slower main memory. Below that is primary or main memory — the RAM and ROM on the motherboard, which the processor can address directly. Below that is secondary storage — hard disks, solid-state drives, pen drives and discs — which keeps programs and files permanently, but which the processor cannot read directly: their contents must first be copied into RAM. At the bottom is tertiary or offline storage, such as magnetic tape, meant for backup and archiving; tape is a serial access medium, so the tape must spool to reach a particular piece of data, unlike a hard disk, which is a direct access medium. The hierarchy works because programs show locality of reference: data used recently is likely to be used again soon (temporal locality), and data stored near recently used data is likely to be needed next (spatial locality) — which is why keeping recent data in cache pays off. The correct order of memory speed, from fastest to slowest, is registers, cache, RAM, hard disk; the papers also ask it in the shorter form cache, RAM, hard disk. The hard disk is the main secondary storage device and the device that can store the most data in most computers; the solid-state drive stores data on flash memory chips, has no moving parts, and is faster than a hard disk, which stores data magnetically on spinning platters. A USB pen drive stores data on flash memory chips. The capacity of a standard single-layer DVD is about 4.7 GB. One byte is the memory needed to store a single alphanumeric character.
The memory hierarchy, fastest to slowest
- Registers — inside the CPU; fastest; a few bytes; hold the data being worked on right now.
- Cache — SRAM between CPU and RAM; very fast; kilobytes to megabytes; holds what the CPU uses most often.
- Main memory (RAM and ROM) — on the motherboard; fast; gigabytes; directly addressed by the CPU.
- Secondary storage — hard disk, SSD, pen drive, discs; slower; gigabytes to terabytes; permanent.
- Tertiary / offline storage — magnetic tape; slowest; backup and archiving; serial access.
The named registers
- Program Counter — holds the address of the next instruction to be carried out.
- Accumulator — holds the result of an arithmetic or logical operation.
- Memory Address Register (MAR) — holds the address of the memory location being accessed.
- Memory Buffer Register / Data Register — holds the data being moved to or from memory.
- Instruction Register — holds the instruction currently being carried out.
The ladder rule
- Closer to the processor: faster, smaller, costlier per byte, more volatile.
- Further from the processor: slower, larger, cheaper per byte, more permanent.
- Locality of reference is why the ladder works: temporal locality (recently used data is reused) and spatial locality (nearby data is needed next).
Units of memory: from a bit to a terabyte — and beyond
Computer memory is measured in bytes, and the units are powers of 1024. The smallest unit of data is the bit, which can have two possible values, 0 and 1. Eight bits make one byte, and one byte stores a single alphanumeric character. Four bits make one nibble, half a byte. From there the ladder climbs by 1024 at every step: 1024 bytes make one kilobyte, 1024 kilobytes make one megabyte, 1024 megabytes make one gigabyte, 1024 gigabytes make one terabyte. The numbers the papers ask most are the ones to learn as a table: 1 kilobyte is 1024 bytes, and therefore 8192 bits; 1 megabyte is 1024 kilobytes, which is 1,048,576 bytes; 1 gigabyte is 1024 megabytes, which is 1,073,741,824 bytes; 1 terabyte is 1024 gigabytes, which is 1,099,511,627,776 bytes — about 1.1 trillion bytes. The arithmetic questions follow from the same ladder: 3.5 gigabytes are 3584 megabytes, and 1 gigabyte is 1,048,576 kilobytes. Above the terabyte the ladder continues in the same steps: 1 petabyte is 1024 terabytes, 1 exabyte is 1024 petabytes, and then come the zettabyte and the yottabyte, so the zettabyte is larger than the exabyte, the petabyte and the terabyte. The largest unit among the four the papers offer — kilobyte, megabyte, gigabyte, terabyte — is the terabyte. One trap needs care, because a real paper fell into it: in the decimal (SI) system used by disk makers, 1 kilobyte is 1000 bytes and 1 terabyte is 1000 gigabytes, while computers count in binary, where 1 kilobyte is 1024 bytes and 1 terabyte is 1024 gigabytes. The binary units are properly called kibibyte, mebibyte and gibibyte, but the papers almost always use the binary values — kilobyte 1024, megabyte 1024 kilobytes, gigabyte 1024 megabytes, terabyte 1024 gigabytes — and a question that keys 1000 gigabytes for a terabyte is marked wrong on this page, with the reason given in full. That is also why a disk sold as 1 terabyte shows less than 1024 gigabytes in the operating system: the maker counted in thousands and the computer counts in 1024s.
The units ladder (binary — the one the papers use)
- 1 bit — the smallest unit; two possible values, 0 and 1.
- 1 nibble = 4 bits; 1 byte = 8 bits; 1 byte stores one alphanumeric character.
- 1 kilobyte (KB) = 1024 bytes = 8192 bits.
- 1 megabyte (MB) = 1024 KB = 1,048,576 bytes.
- 1 gigabyte (GB) = 1024 MB = 1,073,741,824 bytes.
- 1 terabyte (TB) = 1024 GB = 1,099,511,627,776 bytes.
- Above the terabyte: 1 petabyte (PB) = 1024 TB; 1 exabyte (EB) = 1024 PB; then zettabyte, yottabyte.
The sums the papers set
- 3.5 GB = 3584 MB.
- 1 GB = 1,048,576 KB.
- 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.
The 1000 against 1024 trap
- Binary (computers, and the papers): 1 KB = 1024 bytes; 1 TB = 1024 GB.
- Decimal / SI (disk makers): 1 KB = 1000 bytes; 1 TB = 1000 GB.
- Binary units are properly called kibibyte (KiB), mebibyte (MiB), gibibyte (GiB).
RAM: the volatile working memory
Random Access Memory is read-write memory: the processor can both fetch from it and write into it, and every location takes the same time to reach, which is exactly what random access means. RAM is the main memory of the computer and the memory that is directly accessed by the CPU; it is the internal, temporary location where data is processed and stored while the computer is running, and when someone says a computer does not have enough memory, RAM is what is meant. RAM is volatile: it loses its data when the power is switched off, so it is also called temporary memory, and data in RAM is lost when the computer is turned off. That is the single most-asked fact about RAM, and its opposite — ROM keeps its data — is the most-asked fact about ROM. RAM is also called both read memory and write memory, because the computer can both read from it and write to it. RAM comes in two families, and the papers ask the difference between them as a pair. Static RAM stores each bit in a flip-flop made of six transistors, needs no refreshing, is very fast and very costly, and is therefore used as cache. Dynamic RAM stores each bit as a charge on a tiny capacitor reached through one transistor; the charge leaks away in milliseconds, so the circuit must refresh every cell thousands of times a second. DRAM is slower than SRAM but far denser and cheaper, which is why the main memory of every computer is DRAM. The type of RAM that needs constant refreshing is DRAM. SDRAM is Synchronous Dynamic Random Access Memory: DRAM that works in step with the system clock, and DDR, DDR2, DDR3, DDR4 and DDR5 are its successive generations. DDR stands for Double Data Rate: the memory transfers data twice per clock cycle, once on the rising edge and once on the falling edge, so it moves twice as much data as ordinary SDRAM at the same clock speed. RAM, cache and the registers are all volatile — a question that asks which of RAM, ROM and cache is volatile has the answer 'both RAM and cache' — while ROM and PROM are non-volatile. The memory hierarchy's fastest memory is the registers, then cache, then RAM; where the options do not include registers, cache is the fastest answer, and where they do not include cache either, RAM is.
SRAM against DRAM
- SRAM — one bit in a flip-flop of six transistors; no refreshing; very fast; very costly; used as cache.
- DRAM — one bit as a charge on one capacitor plus one transistor; must be refreshed constantly; slower; dense and cheap; used as main memory.
- SDRAM — Synchronous DRAM, in step with the system clock; DDR, DDR2, DDR3, DDR4 and DDR5 are its generations.
- DDR — Double Data Rate: transfers data twice per clock cycle.
RAM, in one line each
- RAM stands for Random Access Memory.
- RAM is volatile — its contents are lost when the power is switched off; it is temporary memory.
- RAM is the main memory, directly accessed by the CPU; it is also called read memory and write memory.
- RAM and cache are both volatile; ROM and PROM are non-volatile.
ROM and its family: PROM, EPROM, EEPROM and flash
Read Only Memory is non-volatile: it keeps its data even when the power is switched off, so it is the permanent memory of the computer, and the memory used to store the BIOS program — the Basic Input/Output System, the firmware that starts the computer, tests the hardware and loads the operating system. ROM is the memory that retains data when the computer is switched off and the memory that stores data permanently even when the power is off; a question that asks which memory is permanent has the answer ROM, and a question that asks which type of memory is non-volatile has the same answer. The BIOS settings themselves — the boot order, the system clock — are kept in CMOS memory, which is powered by a small coin-cell battery on the motherboard; if that battery dies, the settings are lost and return to their defaults. ROM is not one thing but a family, and the family is ordered by how the chip can be written and erased. Mask ROM is programmed during manufacture and can never be changed. PROM, Programmable Read-Only Memory, is supplied blank and is programmed once by the user with a special programmer, which blows tiny fuses inside the chip; once programmed it cannot be erased or altered. EPROM, Erasable Programmable Read-Only Memory, can be programmed, erased and reprogrammed many times: it is erased by exposing the chip to ultraviolet light through the small window on it, which clears the charge from its floating gates. EEPROM, Electrically Erasable Programmable Read-Only Memory, is erased and reprogrammed electrically, one byte at a time, without removing the chip from the circuit — which is why it stores BIOS settings and firmware that must be updated in place. It is the type of memory that can be erased and reprogrammed using electricity. Flash memory is a type of EEPROM, but it is erased and written in blocks, typically 512 bytes at a time, instead of one byte at a time, so it is much faster; it is the memory in USB pen drives, memory cards and solid-state drives. Firmware is software stored permanently in ROM or flash that controls a piece of hardware; the BIOS is the best-known example. The family, in order of flexibility, is mask ROM (never changeable), PROM (once), EPROM (many times, by ultraviolet light), EEPROM (many times, electrically, byte by byte), flash memory (many times, electrically, block by block).
The ROM family, in order of flexibility
- Mask ROM (MROM) — programmed during manufacture; can never be changed.
- PROM — Programmable ROM; programmed once by the user with a programmer; cannot be erased.
- EPROM — Erasable Programmable ROM; erased by ultraviolet (UV) light; reprogrammed many times.
- EEPROM — Electrically Erasable Programmable ROM; erased and rewritten electrically, one byte at a time, in place.
- Flash memory — a type of EEPROM; erased and written in blocks, so much faster; in pen drives, memory cards and SSDs.
ROM, in one line each
- ROM stands for Read Only Memory; it is non-volatile and permanent.
- ROM stores the BIOS program; BIOS is the Basic Input/Output System.
- CMOS memory stores the BIOS settings and is kept alive by a small battery on the motherboard.
- Firmware is software stored permanently in ROM or flash that controls a piece of hardware.
Cache memory: the fast buffer between CPU and RAM
Cache memory is a small, very fast memory that sits between the CPU and the main memory and stores the data and instructions the CPU uses most often, so the processor can get them quickly instead of waiting for the slower RAM. It acts as a buffer between the CPU and the main memory, and its main job is to store the data the processor uses often so it can be reached quickly. Where the options do not include the registers, cache is the fastest memory in the computer; it is built into the CPU, and the type of memory that is fastest and built into the CPU is cache. Cache is built from SRAM, which is why it is so fast and so costly, and why there is so little of it — kilobytes to a few megabytes against the gigabytes of RAM. Modern processors divide cache into three levels. L1 cache is the smallest and fastest level, built into the processor core itself, closest to the core, and usually split into separate instruction and data caches; L2 cache is larger and slower and is private to each core; L3 cache is the largest and slowest cache level and is shared between all the cores on the chip — it is the last-level cache, the common pool a request passes through before it goes to main memory. A cache does not move single bytes: it moves fixed-size blocks called cache lines, and each line carries a tag that says which block of main memory it holds. Where a block may go is decided by the mapping function, and there are three kinds. In a direct-mapped cache each block of main memory maps to exactly one cache line, fixed by its address: lookup is fast, but two blocks that map to the same line keep evicting each other. In a fully associative cache any block can go into any cache line, so there are no conflicts, but the cache must compare the address against every line. In a set-associative cache the cache is divided into sets of several lines, a block maps to one set and may occupy any line inside that set — the compromise most processors use. When a set is full and a new line arrives, a replacement policy chooses the victim, usually the least recently used line. Two more facts complete the picture. A cache hit means the data the CPU asked for was already in the cache; a miss means it had to be fetched from main memory, and the hit ratio is cache hits divided by total accesses — 90 hits out of 100 accesses is a 90% hit ratio. And when the CPU writes, a write-through cache writes the change to both the cache and main memory at once, while a write-back cache writes to the cache only, marks the line dirty, and writes it out to main memory only when the line is evicted.
The cache levels
- L1 — smallest and fastest; built into the processor core; private to each core; often split into instruction and data caches.
- L2 — larger and slower than L1; private to each core.
- L3 — largest and slowest cache level; shared between all the cores; the last-level cache.
The three mapping functions
- Direct-mapped — each main-memory block goes to exactly one cache line.
- Fully associative — any block can go into any cache line.
- Set-associative — a block maps to one set and may occupy any line inside that set; the usual compromise.
Hits, misses and writes
- Hit — the data was already in the cache; miss — it had to be fetched from main memory.
- Hit ratio = cache hits / total accesses (90 hits in 100 accesses = 90%).
- Write-through — every write goes to both cache and main memory.
- Write-back — writes go to the cache only; the dirty line is written out when evicted.
- Cache is built from SRAM; it moves data in fixed-size blocks called cache lines.
Virtual memory: paging, page faults and the swap file
Virtual memory is a technique that lets a computer run programs larger than its physical RAM, by using part of the hard disk as if it were extra RAM. It is memory on a hard disk that is used as extra RAM, and it is primarily stored on the hard disk, in a page file or swap area. Every program is given its own virtual address space — the addresses the program uses — which the memory management unit translates into physical addresses, the places in RAM where the data really sits. The translation is done by paging: virtual memory is divided into fixed-size blocks called pages, typically 4 KB each, and physical memory is divided into page frames of the same size; the page table maps each page to the frame that holds it, and the offset inside the page is unchanged by the translation. A page is a fixed-size block of virtual memory; a frame is the physical block of the same size that can hold it. Only the pages a program actually uses need to be in RAM. When a program asks for a page that is not currently in main memory, a page fault occurs, and the operating system must load the missing page from the disk into a free frame of RAM before the program can continue. Loading every page up front would waste memory, so demand paging loads a page only when the program actually needs it; pages that are never used are never loaded. Because the page table is large — a 32-bit address space with 4 KB pages has over a million pages — the processor keeps a small, very fast cache of the most recent translations, the Translation Lookaside Buffer (TLB), so it usually does not have to read the full page table in RAM. The area on the disk that holds pages not currently in RAM is called swap space, and the disk is used as an extension of RAM for virtual memory. Segmentation is the alternative to paging: instead of fixed-size pages, the program is divided into variable-size segments that follow its logical parts — code, data and stack. When the system demands too many pages at once, it starts swapping pages in and out constantly and spends most of its time paging instead of doing useful work; that condition is called thrashing, and adding more RAM is the usual cure. Virtual memory is also what gives each program its own private address space, so one program cannot see or corrupt another program's memory.
Virtual memory, in one line each
- Virtual memory — part of the hard disk used as extra RAM, so programs larger than physical RAM can run.
- Page — a fixed-size block of virtual memory (typically 4 KB); frame — the physical block of the same size.
- Page table — maps each virtual page to the physical frame that holds it.
- Page fault — the program asked for a page not currently in main memory; the operating system loads it from disk.
- TLB — a small, very fast cache of recent page-table translations.
- Swap space — the area on the hard disk that holds pages not currently in RAM.
- Demand paging — a page is loaded only when the program actually needs it.
- Segmentation — the program divided into variable-size segments (code, data, stack), not fixed-size pages.
- Thrashing — the system spends most of its time swapping pages instead of doing useful work.
More facts to learn
- Which of the following is an example of secondary storageHard disk drive
- In a computer, which register holds the address of the next instruction to be carried outProgram Counter
- Which is the fastest memory inside a computer's processorRegisters
- Which type of memory is used in a USB pen driveFlash memory
- Which of the following stores the most dataHard disk
- In the memory hierarchy the fastest memory isRegisters
- Which of the following is the correct order of computer memory from the fastest to the slowestRegisters, cache, RAM, hard disk
- Which register holds the address of the memory location that the CPU is about to read from or write toMemory Address Register
- What is 1 gigabyte equal to1024 megabytes
- What does 1 kilobyte (KB) equal in computing1024 bytes
- How many bits are there in 1 nibble4
- One megabyte (MB) is equal to how many kilobytes (KB)1024 KB
- How many kilobytes are there in one megabyte1,024 kilobytes
- How many bytes make one kilobyte1024 bytes
- One byte has8 bits
- One nibble has how many bits 4. 1 gigabyte is equal to 1024 megabytes. 1 terabyte is equal to how many gigabytes1024 gigabytes
- State the number of kilobytes are there in one megabyte.1024
- One byte is equal to how many bits8 bits
- How many megabytes are there in 3.5 gigabytes3584
- One terabyte is equal to how many gigabytes1024
- One (1) KB (Kilobytes)1024 bytes. 1 GB is equal to 1024 MB
- One megabyte contains how many kilobytes1024 KB
- How many kilobytes are there in 1 MB1024 KB
- Which of the following is the largest unit of data storageTerabyte. 1 kilobyte equals how many bytes 1024. 1 KB is equal to how many bytes 1024
- One exabyte (EB) is equal to how many petabytes (PB)1024 PB
- How many bytes are there in one terabyte (TB)1,099,511,627,776 bytes
- Which internal temporary location of the computer is used to process and store dataRAM
- Which type of computer memory is called volatile because it loses its data when the power is offRAM
- What does the hard disk drive storeFiles and programs permanently
- Which memory is volatile, losing its data when the power is offRAM
- Which memory is called temporary memoryRAM
- Which type of RAM needs constant refreshingDRAM
- RAM is the abbreviation ofRandom Access Memory
- Which type of storage retains data after the power is switched offNon volatile
- RAM, in computing, stands for whatRandom Access Memory
- In computing, what do the letters RAM stand forRandom Access Memory
- What does SDRAM stand forSynchronous Dynamic Random Access Memory
- Why must DRAM be refreshed constantlyBecause the charge stored in its capacitors leaks away
- Which type of computer memory does NOT lose its data when the computer is switched offROM
- Which memory stores the BIOS settings of a computerCMOS memory
- Which memory is used to store data permanentlyROM
- What does “ROM” stand forRead Only Memory
- ROM is which type of memoryNon-volatile
- Which memory is permanentROM
- PROM (Programmable Read-Only Memory) can be programmed:Only once, after manufacture
- Flash memory differs from EEPROM mainly in that it is erased:In blocks, which makes it faster
- Which memory sits between the CPU and the main memory and stores the data the CPU uses most oftenCache memory
- What is the main job of the cache memory in a computerTo store data the processor uses often, so it can get it quickly
- Which of the following is a volatile memory? RAM, ROM, Cache, Both A and CBoth A and C. Which memory is located between the CPU and RAM and stores frequently used data Cache memory
- Which level of cache memory is shared between all the cores of a processorL3 cache
- In a set-associative cache, a block of main memory can go into:Any line inside its own set
- Cache memory is built from which type of RAMSRAM
- Virtual memory is primarily stored inHard Disk
- What is the TLB (Translation Lookaside Buffer)A small, very fast cache of recent page-table translations
- Swap space is located whereOn the hard disk
- The CMOS memory that stores the BIOS settings is kept alive when the computer is off by:A small battery on the motherboard