Linux Virtual Memory: Optimizing Virtual Memory on Linux

Enterprise Networking Planet content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Virtual memory is one of the most essential elements of an operating system (OS). Linux virtual memory works differently than other OS.

Linux virtual memory uses different techniques, such as copy-on-write, demand paging, and page aging in order to improve performance by offloading redundant or unnecessary processes from the disk.

This article will explain how Linux virtual memory works and provide a brief tutorial on how you can set up virtual memory on your own Linux machine.

How virtual memory works in Linux

Virtual memory is generally thought of as only used to extend a system’s physical RAM. However, Linux techniques and virtual memory components execute more sophisticated tasks.

Linux virtual memory allows each process to have its own private address space, even though there may not be enough physical memory to map all the addresses in all processes simultaneously.

It achieves this using a technique known as “paging.” Paging basically swaps memories from the physical memory to disk storage and back, depending on how frequently they are used.

Paging also gives each process the exact amount of memory it needs. For example, when a new process is created, the OS creates a virtual address space. But because this address space is “bigger” than the process itself, the OS will identify the pages of memory that the process needs. Then all the pages that the process does not require will be kept on disk in a swap file or swap partition.

Linux virtual memory techniques

Using Linux virtual memory techniques, users can run more processes than would normally be possible, run processes simultaneously, or use all of the available physical memory.

Linux virtual memory techniques include copy-on-write, demand paging, and page aging.

  • Copy-on-write: When creating new pages of memory, Linux does not allocate new physical memory for a page but creates a virtual address for the page and marks the page as “shared.” When the process tries to write to the page, the OS only writes to the physical memory if the page is not already in use by another process. This can save a lot of physical memory, especially if multiple processes are using the same data.
  • Demand paging: The only pages of memory that are loaded by Linux into the physical memory are those that a process demands or attempts to access. The benefits of this technique include speeding performance by not loading pages that are not often needed.
  • Page aging: Linux virtual memory keeps track of every page of memory being used. The inactive or less accessed are likely to be swapped out to disk, while the most used ones are kept in the physical memory.

The Buddy and the Slab Allocator

The Linux kernel has two main memory allocators: the Buddy Allocator and the Slab Allocator.

The Buddy Allocator

The Buddy Allocator interacts directly with the Memory Management Unit (MMU), providing valid pages when the kernel asks for them. It also manages lists of pages and keeps track of different categories of memory addresses.

The Buddy Allocator is a general-purpose memory allocator and can be used to allocate memory of any size. It works by dividing memory into a binary tree of blocks. Every time a process requests memory, it will find the smallest block that is large enough to meet the demands of the request and allocate the block to the process.

The Slab Allocator

The Slab Allocator is a layer of the Buddy Allocator and provides the ability to create a cache of objects in memory. It is used to allocate memory for small objects.

The Slab Allocator groups objects of the same size together into a slab. When a process requests an object, the Slab Allocator checks the cache to determine if the object is already in memory.

If the object is in memory, the Slab Allocator returns the object to the process. If the object is not in memory, the Slab Allocator allocates a new object from the cache and returns the object to the process.

Linux memory pages

Linux classifies different types of pages in the virtual memory as:

  • Free pages: Pages of memory not used by any process at the time and available to be allocated to a process that needs them.
  • Active pages: Pages of memory in use by one or more processes. These are not available to other processes until they are no longer being used.
  • Inactive pages: Pages of memory that are not being used by any process and have not been modified since they were last read from disk. These pages are available to be swapped out to disk to free up physical memory.
  • Dirty pages: Refers to pages in use by one or more processes and which have been recently modified. To be swapped, these pages must be written.

Linux kernel tasks

Linux also has a number of kernel tasks for specific aspects of virtual memory management.

For example, the page fault handler, responsible for managing page faults, is activated when a process attempts to access a page of memory that is not in physical memory. The page fault handler brings the page from the disk into physical memory and resumes the execution of the process.

The memory pager is responsible for swapping pages of memory to disk. It is used by the OS when it needs to free up physical memory. The memory pager selects the pages of memory that are swapped by identifying which are not being used frequently.

Similarly, the page reclaimer brings pages of memory that have been swapped out to disk back into physical memory. It is activated when a process requests access to these pages.

Setting up virtual memory in Linux with tunable parameters

Tunable parameters are kernel settings that can be adjusted to improve the performance of your system. They can be adjusted in real-time using the sysctl command or in the /etc/sysctl.conf file.

Remember that changing parameters can have negative impacts on your system and affect your performance.

Let’s look at the most important tunable parameters for virtual memory.


vm.swappiness is a parameter that controls how aggressively the kernel will swap out pages of memory to disk.

When set at a value of 0, the kernel will never swap out pages of memory. If set at 100, the kernel will swap out pages of memory as soon and as rapidly as they are no longer in use. The value set by default is 60.

For example, to set vm.swappiness to 10, you would use the following command:

sysctl -w vm.swappiness=10


With this parameter, users can control how full the kernel’s writeback cache can get before it starts writing dirty pages to disk.

Once again, if vm.dirty_ratio is set to 0, the kernel will never write dirty pages to disk. Set to 100, it will write dirty pages to disk as soon as they are dirty. The system default value is 20.

To change this parameter to 80 in real-time, with immediate effect, use the following command:

sysctl -w vm.dirty_ratio=80


vm.dirty_background_ratio defines how full the kernel’s writeback will get before it starts writing dirty pages to disk in the background.

Set at 0, no dirty pages will be written in the background, while when set at 100, the kernel will write dirty pages to disk in the background as soon as they are dirty. The default value is 10.

The code to set the parameter at 25 is:

sysctl -w vm.dirty_background_ratio=25

This will immediately switch the parameter to 25. Using this example, the kernel will only start writing dirty pages to disk in the background when the writeback cache is 25% full. This ensures dirty pages are not written to disk too late.

If the writeback cache is too full, the kernel may have to start writing dirty pages to disk in the foreground, causing noticeable slowdowns.

What are the best practices for managing memory in Linux?

There are several techniques and good approaches to take when managing virtual memory in Linux systems.

You can use a swap file or a partition, a portion of your hard drive, to store pages of memory that are not in use by any process. This improves performance by freeing up physical memory.

You can also check the amount of physical memory on your system (free -h), determine the amount of swap space you need (free -h | grep -i swap), and check the status of your virtual memory (free -h | grep -i swap).

Other common good practices, such as keeping your computer in good shape, closing applications you are not using, and deleting unnecessary files, can also help improve your virtual memory management.

It’s critical that you keep your Linux OS updated constantly, as the Linux kernel is constantly releasing new features and bug fixes.

Bottom line: Using Linux virtual memory to improve performance

Linux virtual memory is an excellent feature, but understanding how it works is critical to improving its performance.

Armed with the information in this article—from how it works, how it defines and classifies pages, and what techniques it utilizes to the most used parameters to better configure a system—you should be able to start implementing this valuable feature.

Always remember that resetting parameters can negatively impact your system and requires testing, but if you are careful and take the right steps, you can dramatically increase the performance.

We analyzed, selected, and reviewed the best network virtualization software to further boost server performance.

Ray Fernandez
Ray Fernandez
Ray is a Content and Communications Specialist with more than 15 years of experience. He currently works at Publicize and as a contributing writer for TechRepublic, eSecurityPlanet, and ServerWatch in addition to Enterprise Networking Planet. His work has also been published in Microsoft, Venture Beat, Forbes, Entrepreneur, The Sunday Mail, FinTech Times, Spiceworks, Dice Insights, Horasis, the Nature Conservancy, and other leading publications.

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends, and analysis.

Latest Articles

Follow Us On Social Media

Explore More