Your Linux system secretly uses your hard drive as RAM, and that’s a good thing


If you spend time around serious Linux users, you’ll likely hear a reference to “swap”. Or maybe you’ve wondered what a swap section is Your Linux installer was created. Virtual memory and swap space can be confusing. Here’s what you need to know about Linux.

What is virtual memory?

A more flexible view of storage

Hand holding LPCAMM2 RAM memory module on top of Framework Laptop 13 Pro. Credit: Framework Computer Inc.

Many people confuse “swap” with “virtual memory”. Virtual memory is a technique that abstracts your computer’s memory over physical RAM.

If you’re familiar with the concept of a virtual machine, where you can create a whole other computer in a program like VirtualBox to run different operating systems, virtual memory is a similar concept. Virtual memory is a representation of your computer’s memory that the operating system uses to present an image of memory that is not limited to the RAM sticks on your computer.

The CPU has a device that makes this possible: the Memory Management Unit, or MMU. The MMU divides memory into “pages”. The MMU also prevents programs from interfering with each other, which is important in a multi-trust, multi-user operating system like Linux.

AMD Ryzen 5 5600 CPU embedded in the motherboard. Credit: Bertel King / How-To Geek

Page sizes are the same size as storage blocks on hard drives and SSDs, which is the main advantage of virtual memory: creating a large, single address space that includes both the system’s RAM and a portion of the hard drive. This approach allows your system to run more and larger programs even if your system doesn’t have enough physical RAM.

The technique was first developed for mainframes and minicomputers that needed to be shared between multiple users. Virtual memory meant less physical memory was needed, allowing customers to save money by buying machines with less physical RAM.

An operating system with memory pages scattered across RAM and disk (I’ll use the term “disk” to refer to main memory, whether on a traditional spinning hard drive or an SSD) can “swap” these pages in and out of main memory. The operating system will attempt to store actively used pages in “swapped” or physical RAM. This is because RAM is traditionally faster than disk, especially in the days when SSDs were readily available.

This approach allows the programs and parts of programs you actually use to be active on the fast RAM instead of the slow disk. Disabling virtual memory can paradoxically mean a slower system, since all active programs must be stored in memory. Many programs will create multiple pages at startup, but the kernel will push them to the background while high-priority pages remain in RAM with virtual memory. According to Linux.com.

If your system doesn’t have enough physical RAM, the OS will constantly swap pages in and out of RAM, unable to keep up, a problem exacerbated by the slow speed of traditional hard drives. This makes the system practically unusable, a problem known as “warping”. Battling around with inadequate RAM has given virtual memory a bad name among power users, or at least people who consider themselves to be power users, with some people even avoiding turning it off entirely. This is a bad idea. It is more efficient to have virtual memory for actively used parts of RAM.

Another major concern with the modern deployment of SSDs is virtual memory Possible premature wear of SSD with lower RAM counts, especially on modern laptops where the RAM is soldered to the motherboard and cannot be upgraded. There is less evidence that this is a practical concern in normal desktop use. If you do intensive work or AAA games, you may want more RAM anyway.

What is a swap area?

Storing pages in files or partitions

The memory area that holds swapped pages is known as swap space.

There are two approaches to implementing swap space in Linux. The most common is to partition a disk. The rule of thumb for swap partitions was twice the physical RAM on the machine. This is what most Linux installations will offer by default when you start partitioning your drive. The disadvantage of this approach is that if you upgrade the RAM, you will also have to enlarge the swap partition. Swap partitions are often the first partitions on a drive because of how traditional hard drives work. It’s faster to search inside the disk, so the installer will locate the swap partition.

Another approach is a swap fileAs the name suggests, it is a special file in the Linux file system. It is more flexible than a dedicated swap partition because it can be easily resized when the physical RAM changes. This is the approach Windows uses for virtual memory. Less common than the dedicated partition approach in the Linux world.

Change partition or change file?

Tradition and flexibility

The custom partition approach seems to be the most common on Linux. Most will probably just do what the Linux installer does. The vast majority of Linux installations still prefer swap partitions.

If you upgrade your RAM often or just prefer a simpler partitioning scheme, you can choose a swap file. It is also possible to run the system without any swap space, but you may find that you run out of memory completely. Linux systems without any modifications are usually found in niche applications such as embedded system controllers.

How much is the swap area?

Rethinking the traditional “twice the physical RAM” rule

The traditional rule of thumb for how much swap space you need is twice as much physical memory. This was from an era when machines had much less RAM than they do today. This can still happen because modern OSes, including Linux, are more capable. Most modern machines support hibernation, and this feature uses swap space to save system state. More swap space is better if you use this feature. You should consider the amount of physical RAM as the minimum swap space.

The only way to correct the blow

System stuttering? You probably need more RAM

A pile of old RAM on the table. Credit: Nick Lewis/How-To Geek

If you suffer from warts, you might want to consider giving it a try adjust the “exchange capacity” of the systemor how aggressively it flips pages in and out. This would only be a temporary fix.

You may not like the answer, esp RAM pricebut the only surefire way to fix the swap is to add more RAM


The unsung hero of Linux

Swap space is something most people on modern Linux systems don’t think about until something goes wrong. Virtual memory makes modern desktops possible, but it can only go so far. Understanding how it works and its limitations will help you make more efficient use of swap space on Linux in your next installation.

Dell XPS 13 Plus 2023

Operating system

Ubuntu Linux 22.04 LTS

CPU

13th generation Intel Core i7-1360P

GPU

Intel Iris Xe Graphics

RAM

16 GB DDR5

Storage

512 GB SSD

Weight

2.71 pounds

The Dell XPS 13 Plus with Linux combines powerful hardware and a great display in a lightweight, good-looking chassis to create a fantastic Linux laptop.




Source link

Leave a Reply

Your email address will not be published. Required fields are marked *