Virtual Memory
- In Turkish
- Sanal Bellek
In short
Virtual memory is an operating system technique that gives each process its own private address space and maps it to physical RAM or disk behind the scenes.
What is virtual memory?
Virtual memory is a way for the operating system to give every process the illusion that it has a large, private, continuous block of memory all to itself. The addresses a program uses are virtual addresses, and the operating system, with help from the CPU, translates them into real locations in physical RAM.
Memory is divided into fixed-size chunks called pages, commonly 4 KB each. A page table for each process records where every virtual page actually lives, and a hardware unit called the memory management unit (MMU) performs the translation on every memory access. If a page is not currently in RAM, the CPU raises a page fault and the kernel loads it, possibly moving a less-used page out to disk in an area called swap space or the page file.
Think of virtual memory like apartment numbers in a building run by a concierge. Each tenant only knows their own apartment numbers, while the concierge keeps a private map of which rooms actually hold their belongings, including some boxes in off-site storage. This design isolates processes from each other, lets programs use more memory than physically exists, and allows shared code such as system libraries to be loaded once and used by many processes.
Virtual memory is often confused with a virtual machine or with swap space. A virtual machine emulates an entire computer, while virtual memory is an addressing scheme inside one operating system. Swap is only one part of virtual memory, and relying on it heavily, known as thrashing, makes a system very slow because disks are far slower than RAM.
Key takeaways
- Each process gets its own private virtual address space.
- The MMU and page tables translate virtual addresses into physical addresses.
- Memory is managed in fixed-size pages, commonly 4 KB.
- Accessing a page that is not in RAM triggers a page fault, and the kernel loads it.
- Virtual memory provides isolation, memory sharing, and the ability to exceed physical RAM.
Example
# Show physical RAM and swap usage
free -h
# Compare this shell's virtual size (VmSize) with the RAM it really uses (VmRSS)
grep -E 'VmSize|VmRSS' /proc/$$/status
# Show the page size in bytes (often 4096)
getconf PAGESIZEReaders ask
Is virtual memory the same as swap?
No. Virtual memory is the whole system of mapping virtual addresses to physical memory, while swap is disk space the operating system can use to hold pages that do not fit in RAM. A system still uses virtual memory even with swap turned off.
What is a page fault?
A page fault happens when a program accesses a virtual page that is not currently mapped to physical RAM. The kernel handles it by loading the page, or, if the address is invalid, by stopping the program with a segmentation fault.
Does virtual memory make my computer faster?
Virtual memory mainly improves safety and flexibility rather than raw speed. It lets more programs run at once, but if the system relies heavily on swap, performance drops sharply.
See also
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
- KernelOperating Systems, p. 17A kernel is the core part of an operating system that manages the CPU, memory, and hardware devices and controls how programs get access to those resources.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
- Virtual MachineDevOps & Cloud, p. 53A virtual machine is a software-based computer that runs its own operating system on shared physical hardware, isolated from other machines on the same host.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- RAMOperating Systems, p. 25RAM (random access memory) is a computer's fast, temporary working memory, holding the programs and data in use; its contents are lost when power goes off.
- Heap MemoryOperating Systems, p. 14Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.
Spotted a mistake or something missing on this page?Suggest an edit