Computer Science › Section B · Data storage › Topic B.2

Main memory

Every running program and every piece of data it is working on sit in main memory, a huge, numbered row of storage cells. Knowing how those cells are organised, addressed and measured explains a lot about how computers behave, from start-up to slowdowns.

A reservoir of bits

The previous topic showed how a single circuit can hold one bit. A computer's main memory is a vast collection of such storage circuits, plus the circuitry that lets the processor put bits in and take them out. It holds the running programs' instructions and the data they are using right now. A simple thermostat's embedded computer may have only a few hundred bytes of main memory; a laptop has billions. The organisation is the same at every size.

Cells, bytes and bit order

Main memory is divided into units called cells, and in almost all modern machines each cell holds 8 bits, a group called a byte. Although a chip has no real left or right, we picture a cell's bits in a row. The left end is the high-order end, and its bit is the most significant bit, because if the cell holds a number, that bit carries the largest place value. The right end is the low-order end, holding the least significant bit. In the byte 10010110, the most significant bit is 1 and the least significant bit is 0.

Addresses

To find a particular cell, each one is given a unique number called its address. Picture all the cells in a single line, numbered from 0 upwards, much as houses on a street are numbered. Because the cells have an order, phrases such as "the next cell" make sense, and the whole memory becomes one long, ordered row of bits. That lets a value longer than one byte be stored in consecutive cells: a 16-bit value takes two neighbouring cells, and a 32-bit value takes four. A processor that uses n-bit addresses can name \(2^n\) different cells.

Reading, writing and random access

The processor uses memory through two operations. A read sends an address and gets back a copy of the cell's contents, leaving the cell unchanged. A write sends an address and a bit pattern, and the new pattern replaces whatever the cell held before; the old value is lost. Because any cell can be reached directly by its address, in any order, in roughly the same time, main memory is called random access memory, or RAM. Disks and tapes, covered in "Mass storage", instead handle data in large blocks.

Worked example: counting addresses

A small controller has 64 KiB of main memory, with one byte per cell. 1. Convert to bytes: 64 × 1,024 = 65,536 cells. 2. Count bits: 65,536 × 8 = 524,288 bits. 3. The addresses run from 0 to 65,535. In hex that is 0x0000 to 0xFFFF. 4. How many address bits are needed? Since \(2^{16} = 65{,}536\), exactly 16 bits name every cell, matching the four hex digits. 5. Check: one more address bit would double the reach to 131,072 cells. The same rule explains a famous limit: 32-bit addresses reach \(2^{32}\) bytes, which is 4 GiB, one reason computers moved to 64-bit addresses.

DRAM, SRAM and volatility

Most main memory is DRAM, dynamic RAM. Each bit is a tiny electric charge stored by a transistor and a capacitor. The charge leaks away within a fraction of a second, so refresh circuitry reads and rewrites every cell many times a second. DRAM is dense and cheap, which is why memory is measured in gigabytes. SDRAM, synchronous DRAM, times its transfers to a clock signal to deliver data faster. SRAM, static RAM, stores each bit in a flip-flop-style circuit that needs no refresh; it is faster but bulkier, so it is used in small, fast memories such as caches. Both are volatile, losing their contents at power-off, so start-up firmware such as UEFI lives in non-volatile read-only memory (ROM) or flash.

Measuring capacity

Memory is built in powers of two, so early engineers used kilobyte for \(2^{10} = 1{,}024\) bytes, close to 1,000. Megabyte and gigabyte followed for \(2^{20}\) and \(2^{30}\) bytes. But kilo, mega and giga already mean exactly 1,000, a million and a billion in every other science. To fix this, standards bodies added binary prefixes in the late 1990s: a kibibyte (KiB) is 1,024 bytes, a mebibyte (MiB) is \(2^{20}\) bytes and a gibibyte (GiB) is \(2^{30}\) bytes, while kB, MB and GB keep their decimal meanings. In everyday use, memory sizes in GB still usually mean the binary values, so always check which is intended.

Worked example: storing a 16-bit value

A program stores the 16-bit value 0x3A7C at address 200. 1. Split it into bytes: the high-order byte is 0x3A (00111010) and the low-order byte is 0x7C (01111100). 2. Two consecutive cells are needed: addresses 200 and 201. 3. Big-endian order puts the high-order byte first: cell 200 holds 0x3A and cell 201 holds 0x7C. 4. Little-endian order puts the low-order byte first: cell 200 holds 0x7C and cell 201 holds 0x3A. Many common processors, including the x86 family, are little-endian. 5. Check: reading the cells in the wrong order gives 0x7C3A, a different number, so programs exchanging binary data must agree on the order.

Caches and locality

Processors run far faster than DRAM can answer, so most systems keep a cache: a small amount of fast SRAM, on or near the processor, holding copies of recently used memory contents. When the data needed is in the cache, it is a hit and the slow trip to main memory is avoided; when it is not, it is a miss. Most processors have several levels: a tiny, fastest L1 cache, then larger, slower L2 and L3 caches. Caches work because programs show locality. Temporal locality means data used recently is likely to be used again soon. Spatial locality means data near recently used data, such as the next item in a list, is likely to be needed next.

Worked example: the value of a cache

Use illustrative figures: a cache hit takes 1 nanosecond (ns) and a miss costs an extra 100 ns to fetch from DRAM. 1. Suppose 95% of accesses hit, so 5% miss. 2. Average access time = hit time + miss rate × miss penalty. 3. Substitute: 1 + 0.05 × 100 = 1 + 5 = 6 ns. 4. Without the cache, every access would take about 100 ns. 5. Compare: 100 ÷ 6 ≈ 17, so memory appears about 17 times faster. 6. At a 90% hit rate the average rises to 1 + 0.10 × 100 = 11 ns, which is why code with good locality runs faster.

Common mistakes to avoid

First, confusing an address with the contents stored there: cell 5 might hold the value 200. Second, thinking a read empties a cell; reads copy, and only writes replace. Third, assuming RAM keeps data after power-off: DRAM and SRAM are both volatile. Fourth, treating GB and GiB as identical; they differ by about 7%. Fifth, forgetting that a value longer than a byte spans several consecutive cells, in an order that depends on the machine.

Key takeaways

Main memory is an ordered row of cells, usually one byte each, with addresses starting at 0; n-bit addresses reach \(2^n\) cells. Each byte has a most significant (high-order) and least significant (low-order) bit. Reads copy a cell; writes overwrite it. RAM is random access: any cell, any order. DRAM stores charges that need refreshing; SRAM is faster and used for caches; both are volatile, so firmware lives in non-volatile ROM or flash. KiB, MiB and GiB are powers of 1,024; kB, MB and GB are strictly powers of 1,000. Multi-byte values are big-endian or little-endian. Caches exploit locality.

Flashcards

Flashcard

Main memory

The computer's working store for running programs and their data

Built from many storage circuits.

Test your understanding

Question 1 of 5

A device's memory has 2¹⁸ one-byte cells. How many address bits does it need, and what is the highest address?

Question 2 of 5

Cell 40 holds 0x1F. The processor writes 0x2B to cell 40 and then reads cell 40 twice. What do the two reads return?

Question 3 of 5

On a little-endian machine, the 16-bit value 0x5C21 is stored at addresses 300 and 301. What does cell 300 hold?

Question 4 of 5

Why must DRAM be refreshed many times a second?

Question 5 of 5

A cache has a hit time of 2 ns, a miss penalty of 80 ns and a hit rate of 90%. What is the average access time?

← Previous topicNext topic →