Linux › Section A · The big picture › Topic A.2

Hardware: main memory and state

The kernel, every running program and every piece of data being worked on exist as patterns of bits in main memory. Seeing memory this way explains what people mean by the state of a system, and why the kernel spends so much effort managing memory.

Everything running lives in memory

Of all the hardware in a Linux machine, main memory matters most for understanding how the system works. While the computer is on, the kernel's code and data sit in memory, and so does every process: its instructions, its variables and the files it has open in buffers. When you start a program, its code is copied from disk into memory before it can run. When you switch the machine off, the contents vanish, which is why every boot must load the kernel again from storage.

Bits, bytes and addresses

Main memory, or RAM, is a long row of storage cells. Each holds 8 bits, a byte, and has a numbered address, so the processor can reach any cell directly. The Computer Science topic "Main memory" covers cells, addresses and the DRAM chips that store the bits; here the key points are two. First, memory is fast but volatile: it keeps its contents only while powered. Second, it is limited, so it must be shared out among the kernel and all the processes. Sharing it safely, so that no process can read or overwrite another's memory, is one of the kernel's main jobs.

The processor works on memory

A CPU can be pictured as an operator on memory. It fetches an instruction from memory, carries it out, and moves on to the next. Many instructions read data from memory into the processor's registers, a few very fast storage slots inside the CPU, compute with it, and write results back. Small caches near the processor keep copies of recently used memory so that this traffic is quicker, but they are only copies: main memory holds the authoritative contents. Whether it runs the kernel or a browser, the processor is always reading and changing bits in memory.

Input and output pass through memory

Devices exchange data with programs through memory too. When a program reads a file, the kernel first copies the disk blocks into memory, into an area called the page cache, and then gives the program its bytes from there. A network card places each arriving packet into a memory buffer, often by direct memory access (DMA), in which the device writes to memory without the processor copying each byte. Output works the same way in reverse: data is assembled in memory and then handed to the device. This is why memory is the meeting point of the whole system.

What state means

You will often hear about the state of memory, of a process or of the kernel. Strictly, a state is one particular arrangement of bits. If a tiny memory had four bits, then 0110 and 1011 would be two different states of it. Each extra bit doubles the number of possible arrangements, so n bits can be in \(2^n\) different states. Changing even one bit gives a new state, which is why a single corrupted bit can make a program misbehave. Every action the processor takes moves the system from one state to the next.

Worked example: counting states

How many states can different amounts of memory have? 1. Four bits: \(2^4 = 16\) states, from 0000 to 1111. 2. One byte, 8 bits: \(2^8 = 256\) states. 3. One 4 KiB block, the size of a typical memory page on x86-64 Linux: \(4096 \times 8 = 32{,}768\) bits, so \(2^{32768}\) states. 4. Since 32,768 × log₁₀ 2 is about 9,864.3, that number has 9,865 decimal digits. 5. Check: a real process uses millions of such blocks. Listing a process's state bit by bit is therefore hopeless, which is exactly why people describe states in words instead.

Describing state in words

Because raw bit patterns are unreadable at this scale, we describe states by what something is doing. You might say a process is waiting for keyboard input, is running on the CPU, or has finished but not yet been cleaned up. These are abstractions: each covers an enormous number of possible bit patterns that all mean the same thing to us. The kernel keeps a short record of each process's condition in this style, and tools such as ps report it as a one-letter code. The topic "Listing and manipulating processes: signals and jobs" shows how to read those codes.

Images: state captured as a pattern

The word image means a particular arrangement of bits, often one saved so it can be reproduced later. The kernel image is the file holding the kernel's program, often named /boot/vmlinuz followed by a version, which the boot loader copies into memory at start-up. A disk image is a file containing the exact contents of a whole disk or partition, used for backups and virtual machines. A core dump is an image of a crashed process's memory, saved for debugging. In each case, restoring the same bits restores the same state.

Looking at memory on Linux

Linux reports memory use through the file /proc/meminfo, which the kernel generates on demand; fields such as MemTotal, MemFree and MemAvailable appear there. The free command summarises the same figures. By default it shows kibibytes (units of 1,024 bytes); free -h scales them to readable units such as Mi and Gi, which are powers of 1,024, and adding --si switches to powers of 1,000. Its free column is memory holding nothing at all. Its available column estimates how much could be given to new programs without swapping, counting cache the kernel can drop. In current versions, used is total minus available.

Worked example: reading free output

A laptop's free -m shows this illustrative line: Mem: total 15872, free 1536, buff/cache 9728, available 10752 (all MiB). Is memory nearly full? 1. Only 1,536 MiB is completely unused, which looks alarming. 2. But buff/cache shows 9,728 MiB holding cached file data, most of which the kernel can drop when programs need room. 3. The available figure, 10,752 MiB, is the useful one: about 10.5 GiB could go to new programs. 4. Used is total minus available: 15,872 − 10,752 = 5,120 MiB, or 5 GiB. Check: 15,872 MiB is 15.5 GiB, so roughly a third is truly in use.

Common mistakes to avoid

First, panicking at a small free figure: Linux deliberately fills idle memory with cache, so check available instead. Second, thinking cached files are lost data; the cache is a copy, and the files remain on disk. Third, mixing units: Gi in free -h means \(2^{30}\) bytes, not one billion. Fourth, assuming programs run straight from disk; their code is loaded into memory first. Fifth, treating state as something vague; strictly it is an exact bit pattern, and words are only a convenient summary of it.

Key takeaways

A running Linux system is bits in main memory: the kernel, every process and the data in transit. Memory is fast, limited and volatile, so the kernel must share it safely. The CPU fetches instructions and data from memory and writes results back, and device input and output pass through memory buffers and the page cache. A state is an exact arrangement of bits, and n bits have \(2^n\) states, so we describe states in words. An image is a saved bit pattern, such as a kernel image or disk image. /proc/meminfo and free report memory; available matters more than free.

Flashcards

Flashcard

Main memory (RAM)

Fast, volatile storage holding the kernel, processes and their data

Emptied at power-off.

Test your understanding

Question 1 of 5

How many different states can 6 bits of memory be in?

Question 2 of 5

free -m shows total 7936, free 512, buff/cache 4608 and available 5120 (MiB). How much memory does current free report as used?

Question 3 of 5

A server shows very little free memory but a large available figure. What is the best conclusion?

Question 4 of 5

In this topic's sense, what is an image?

Question 5 of 5

A program reads a file from disk. Where does the data go before the program receives it?

← Previous topicNext topic →