free webpage hit counter

What is Virtual Memory in Linux? How to handle it

Virtual memory is one of the things that underlies modern OSes, but usually, you don’t think about it unless you have a problem. Linux distros ask you to set up your virtual memory space (swap partitions) during installation, but most beginners don’t realize how useful that is.

Here’s everything you need to know about virtual memory in Linux.

What is virtual memory?

Virtual memory is a way of representing your memory abstracted from the physical memory on your computer. It uses both your RAM and your storage space, whether it’s a traditional hard drive or an SSD.

In Linux, this is done at the kernel and hardware levels. A CPU has hardware called a memory management unit (MMU) that translates physical memory addresses into virtual ones. These addresses are independent of where they physically reside on the machine. These address spaces are called “pages” and they can be in RAM or on your hard drive or SSD. The OS sees these addresses as a large pool of memory called the “address space”.

Virtual memory takes advantage of the fact that theoretically all the memory used is not used all the time. Programs in memory are broken up into pages, and parts the kernel deems unnecessary are “replaced” or moved to the hard drive. When they are needed, they can be “swapped in” or returned to RAM.

The space on the drive used for virtual memory is called “backing store” or “swap space”. In the Windows world, this is usually implemented as a file called a “swap file”. It is also possible to do this on Linux, but it is more common to use a dedicated disk partition.

Swap files in Linux are usually reserved for minimal or embedded systems, and because embedded OSes need to be small, it’s common to run entirely without virtual memory.

The result of virtual memory is that it is possible to run large programs using more memory than the physical RAM on your computer, similar to how a credit card allows you to make large purchases with more money in your bank account. Like a credit card, virtual memory is useful when you need it but you don’t want to overextend its use.

Virtual memory allows developers to create applications without knowing how the computer’s memory is organized.

Historically the main problem with virtual memory is that hard drives are slower than RAM. If a machine does not have enough RAM, the system may swap pages in and out without leaving them, a process known as “thrashing”. This is less of a problem on modern PCs where more RAM and faster SSDs have displaced mechanical hard drives, but it’s still something to be aware of.

Linux swap partitions

As previously mentioned, the most common way to set up virtual memory in Linux is to use a dedicated disk partition. The installation utility checks your hardware and proposes a partition plan that includes a swap partition.

You can also add swap partitions after installation. If you want to add a new partition to an existing drive, you’ll need to use a destructive partitioning tool like GParted. Make sure you select “Linux Swap” as the filesystem for your partition.

Back up important data before repartitioning your drive.

After you create your partition, use the mkswap command to format your partition.

sudo mkswap /dev/sdX

Now you need to edit yours /etc/fstab As root to add your swap partition. This line, added to the file, sets the swap partition to be mounted at boot time:

/dev/sdX none swap defaults 0 0

Use now Swapan command to activate your new swap space, where sdX is the name of your swap partition:

sudo swapon /dev/sdX

Using swap files in Linux

Setting up a swap file in Linux is easy using the command line. You may want to do this if you don’t mind repartitioning your drive or editing /etc/fstab. Using is a method fall down:

For example, to create a 2GB swap file:

sudo fallocate -l 2G /path/to/swapfile

Alternatively, you can use the dd command to create a swap file.

sudo dd if=/dev/zero of=/path/to/swapfile bs=1024 count=2048

Make sure you are using the dd command correctly, as misplacing infile and outfile can cause data loss. Fallocate is the preferred method for this reason.

The /dev/zero A device is a special device that produces a “0”. This dd command creates an empty two-gigabyte block file suitable for using 1024-kilobyte blocks as swap space.

Then you can use mkswap And Swapan Commands with a swap file as you would with a swap partition:

sudo mkswap /path/to/swapfile
sudo swapon /path/to/swapfile

You may be wondering when you should use a swap file or a dedicated partition for your swap space. The choice is easy: in most cases, you should use a section. It is an excellent choice for a Linux desktop or server. The partition scheme suggested by the installer is usually sufficient on a single-user Linux desktop.

You may want to use a swap file if you’re running Linux in a virtual machine, a small embedded system, or if you don’t want to repartition your existing Linux system.

How much swap space?

For many years, the standard recommendation for how much swap space was needed was twice the amount of physical RAM. With the large drives and amounts of memory that even cheap PCs come with, this rule may come into question.

On many systems, if you check top or htop, you’ll notice that your swap space isn’t even used if you set up your system this way.

However, twice the physical memory is a good starting point and an insurance policy if you make high demands on memory. You can make changes in your system as needed. If your system is using all of your RAM, the computer may experience performance issues when using virtual memory.

The computer can be swapped in and out, a process called thrashing, which causes it to become unresponsive. If you still have a mechanical hard drive, you may often hear it being accessed constantly.

This is less of a problem these days because the amount of RAM on cheap PCs is quite high, and the speed of SSDs is much faster than older hard drives. This is still something you should know.

The easiest way to fix this is to add more RAM to your machine. If that’s not possible, you can try adjusting the “swappiness” of the Linux kernel.

The swappiness number determines how much the kernel sinks into virtual memory. It ranges from 0 to 100. Setting it to 0 means Linux will not swap at all, but at 100, it will swap every chance. The default is 60 on most systems.

To change the swappiness temporarily, use the sysctl command:

sudo sysctl vm.swappiness=20

The “20” in that command will be the swappiness number until you reboot. To change it permanently, edit /etc/sysctl.conf File as root and put the line “vm.swappiness=”[swappiness number]”, where”[swappiness number]” is the swappiness number you want. This is a stopgap until you install more RAM.

Virtual memory keeps your Linux system running smoothly

Virtual memory is a component of modern OSes, including Linux, that keeps your computer running smoothly. You can use a swap file, but the most common method is a dedicated partition. You don’t have to think too much about it, but Linux swap partitions and swap files are easy to set up and troubleshoot.

Although the methods for setting up virtual memory are different, a lot of this advice applies to other systems, including Windows.

Leave a Reply

Your email address will not be published.

Previous post Praised by Prime Minister Modi, artist Ponda now sculpts land art to celebrate Azadi Ka Amrit Mahotsav
Next post SELF-DONE ARTIST | How XTIE has found its place in the music industry