Linux Memory Management Explained: Virtual Memory, Page Cache, Swap, and OOM Killer

Introduction

Understanding how memory management works is essential when learning Linux.

However, even if you have heard terms such as virtual memory, page cache, Swap, and the OOM Killer, you may not have had many opportunities to systematically learn what each mechanism does and how they work together.

For example, after running the free command, you may see very little free memory or notice that Swap is being used and wonder whether the system is actually running out of memory. When a system suddenly slows down or a process is terminated by the OOM Killer, understanding Linux memory management can make a major difference in how quickly you can identify the cause.

Linux provides various mechanisms, including virtual memory, page cache, and Swap, to use limited physical memory efficiently. These mechanisms do not operate independently; they work together to support overall system performance and stability.

In this introductory article in the Linux Memory Management series, we explain the purpose and overall architecture of memory management in Linux. Individual mechanisms such as virtual memory and page cache are covered in more detail in later articles.

By the end of this article, you should have a clear overview of Linux memory management and a solid foundation for learning each mechanism in greater depth.

What Is Linux Memory Management?

Linux memory management is the set of mechanisms used by the Linux kernel to manage physical memory efficiently and safely.

On a server, many processes such as web servers, databases, and application servers may run at the same time. Each process allocates the memory it needs, but physical memory is limited.

Therefore, the Linux kernel is responsible for tasks such as:

  • Allocating memory to processes as needed
  • Releasing memory that is no longer needed
  • Managing caches and Swap to handle memory pressure
  • Protecting processes from unauthorized access to each other’s memory

These are some of the core responsibilities of Linux memory management.

Without proper memory management, one process could overwrite another process’s memory, or physical memory could quickly become exhausted, preventing the system from operating correctly.

Linux combines mechanisms such as virtual memory and page cache to achieve both safety and high performance.

Goals of Memory Management

Linux memory management has four main goals:

  • Use limited physical memory efficiently
  • Provide an independent memory space for each process
  • Prevent unauthorized memory access between processes
  • Maintain overall system performance and stability

For example, it is not unusual for hundreds or even thousands of processes to run simultaneously on a server.

The Linux kernel ensures that each process can safely use the memory it needs while making the most of limited memory resources, for example by using otherwise available memory for the page cache.

When memory becomes scarce, Linux also uses mechanisms such as page reclaim and Swap to avoid immediately exhausting available memory.

These mechanisms allow memory to be used safely and efficiently even when many applications are running at the same time.

Relationship Between the CPU and Memory

When executing a program, the CPU processes instructions and data stored in memory.

When a program starts, its executable code, variables, and other data are placed in memory. The CPU repeatedly fetches instructions, performs operations, and writes results back to memory at high speed.

In other words, memory acts as a working area for the CPU.

No matter how powerful the CPU is, it cannot continue processing without access to the required data. The Linux kernel therefore manages memory allocation, deallocation, and caching so that the CPU can operate efficiently.

For more information about how the CPU executes instructions, see the following article.
How the CPU Works: Instruction Execution (Fetch, Decode, Execute) and Memory Access

User Space and Kernel Space

In Linux, the address space is divided into User Space and Kernel Space.

Applications such as web servers, databases, and Java applications run in User Space.

Kernel Space is where the Linux kernel operates and provides core operating system functions such as:

  • Memory management
  • Process management
  • File systems
  • Network communication
  • Device management

Applications cannot directly access Kernel Space.

When an application needs to read or write files, communicate over a network, or perform operations involving memory, it requests the Linux kernel to perform the operation through system calls.

Separating User Space from Kernel Space helps prevent application failures or unauthorized access from affecting the entire operating system, improving system security and stability.

Independent Address Space for Each Process

In Linux, each process is assigned its own independent virtual address space.

For example, even when a web server and a database run on the same server, each has its own independent memory space.

Therefore, even if different processes use the same virtual address, they cannot directly access each other’s memory regions.

This isolation helps prevent a process crash or invalid memory access from directly affecting other processes.

Linux virtual memory provides this independent address space for each process.

Virtual addresses used by the CPU are translated into physical addresses by the MMU (Memory Management Unit), enabling a separate memory space for each process.

Overview of Linux Memory Management

So far, we have looked at the goals of Linux memory management and how Linux provides an independent address space for each process.

Linux memory management is not implemented by a single mechanism. Multiple mechanisms, including virtual memory, page cache, Swap, and the OOM Killer, work together to provide both safety and high performance.

The following diagram shows an overview of these mechanisms.

The following table briefly explains the key terms.

TermDescription
Virtual AddressA logical memory address used by a process.
Virtual MemoryA mechanism that provides an independent address space for each process.
Anonymous PagesMemory pages that are not backed by files, such as dynamically allocated memory and stacks.
Page CacheA cache that keeps file data in memory to speed up file access.
Physical MemoryThe actual RAM installed in the computer.
Page ReclaimThe process of reclaiming pages to free physical memory.
LRU(Least Recently Used)A mechanism for prioritizing less recently used pages for reclaim.
kswapdA kernel thread that reclaims pages in the background to maintain free memory.
Direct ReclaimSynchronous page reclaim performed in the context of a process when memory allocation cannot immediately be satisfied.
Page Cache ReclaimReleases reclaimable page-cache pages to free physical memory.
SwapMoves less frequently used anonymous pages to swap space to free physical memory.
OOM KillerA mechanism that terminates selected processes to free memory when severe memory pressure cannot otherwise be resolved.

Let’s briefly look at the role of each major component.

Virtual Memory

Virtual memory is a mechanism that provides each process with an independent address space.

The CPU accesses virtual addresses belonging to each process rather than directly addressing physical RAM. The MMU (Memory Management Unit) translates those virtual addresses into physical addresses.

With this mechanism, multiple processes can use the same virtual addresses without directly accessing each other’s memory. Programs can also run without needing to know the actual layout of physical memory.

Virtual memory is a fundamental part of Linux memory management and is essential for providing isolation and flexibility.

The next article explains virtual memory in greater detail, including the MMU, page tables, TLB, and page faults.

Page Cache

The page cache is a caching mechanism used to speed up file access.

Storage access is generally much slower than access to physical memory. Linux therefore keeps file data that has already been read in memory. When the same data is requested again, it can often be served from memory instead of being read from storage again.

Linux actively uses available memory for the page cache. Therefore, even if the free command reports little unused memory, this does not necessarily mean that the system is running out of memory.

Reclaimable page-cache memory can be freed when necessary and reused for other memory demands.

Swap

Swap is a mechanism that can move memory pages from physical memory to swap space on storage under memory pressure.

Depending on memory pressure and kernel settings such as swappiness, less frequently used anonymous pages may be moved to swap space, freeing physical memory for other uses.

Because storage is much slower than physical memory, heavy swapping can reduce application responsiveness.

At the same time, Swap can provide additional flexibility under memory pressure. Therefore, Swap usage should not automatically be treated as a problem; it should be evaluated in the context of workload and performance.

OOM Killer

When Linux cannot satisfy a memory allocation request and reclaim cannot free enough memory, an out-of-memory condition may occur.

In such a situation, the Linux kernel may invoke the OOM Killer (Out Of Memory Killer), which selects and terminates a process to free memory and allow the system to continue operating.

If an application terminates unexpectedly, the OOM Killer may be one possible cause.

During troubleshooting, check dmesg and system logs to determine whether the OOM Killer was invoked.

How Linux Memory Management Mechanisms Work Together

The four mechanisms introduced above do not operate independently.

For example, applications access memory through virtual memory, while file access can benefit from the page cache. Under memory pressure, the Linux kernel reclaims pages and may swap eligible anonymous pages out. If memory allocation still cannot be satisfied, the OOM Killer may be invoked as a last resort.

By coordinating these mechanisms according to system conditions, Linux makes efficient use of limited physical memory while maintaining system stability and performance.

Conclusion

Linux memory management is much more than simply allocating memory. It consists of multiple mechanisms designed to balance isolation, efficient resource use, performance, and system stability.

This article provided an overview of User Space and Kernel Space, independent process address spaces, virtual memory, page cache, Swap, and the OOM Killer.

If you’d like to learn more about Linux memory management, I’ve covered the topic in detail in the following book. Please check it out if you’re interested.

Linux Memory Management Explained

Learn How Linux Works Through Visual Explanations

This book explains how Linux memory management works through clear, visual illustrations, making the concepts easy to understand even for beginners.


View the Book on Kindle

コメント