Introduction.

What is and how does the exchange space work?

The swap space or swap is what is known as virtual memory. The difference between real and virtual memory is that the latter uses space in the storage unit instead of a memory module. When the actual memory is exhausted, the system copies part of the content directly into this swap memory space in order to perform other tasks.

Suggested Reading

Using virtual memory has the advantage of providing the additional memory required when the actual memory has been exhausted and a process has to be continued. As a consequence of using space in the storage unit as memory is that it is considerably slower.

How much swap space should be allocated to the system?

Less than 1 GB RAMDouble the total amount of RAM.
More than 1 GB RAMSame amount of total RAM, plus 2 GB.

Regardless of the amount of available RAM, the system can normally work with 4 to 8 GB of swap memory, unless a particular application or program requires it. Setups for most distributions allocate up to 4 GB if a default installation is performed without customizing even on systems with a lot of RAM.

When is it required to increase the amount of exchange memory?

Consider increasing the amount of swap memory in the following cases:

  • Systems where acquiring additional memory is impossible, and you are aware that the swap memory is much slower than RAM.
  • In teams with intensive work that consumes a lot of memory (graphic design, for example).
  • High performance servers where you want to have a wide margin of exchange space to meet the demands of services.
  • Systems that upgraded from a 2.2 kernel version to a 2.4 or 2.6 kernel version.
  • Systems where the amount of RAM was increased and they are faced with the problem of covering the minimum quota of exchange memory space.

Procedures to increase the amount of swap memory.

Resize the partition.

Resizing partitions is an effective method if there is space available without partitioning. However, it poses a risk due to the possibility of errors during the process that could result in data loss. This method requires having a backup of all important data before starting.

You can use parted or gparted from a live disk.

The best solution for a system that is running out of available memory will always be to acquire more RAM.

Activate an additional swap partition.

The syntax for formatting a partition as swap memory is mkswap with the -f option to force the formatting procedure, the -c option to find, mark and avoid corrupted sectors and the device name as argument:

Mkswap -f -c [device]

Assuming you have an unused partition named / dev / sdb12, the following will format as swap memory to the / dev / sdb12 partition , forcing the procedure and checking sectors for damaged blocks:

Mkswap -f -c / dev / sdb12

The above may return an output similar to the following:

Setting up swapspace version 1, size = 1048576 bytes in 
label, UUID = d2fea5ab-c677-8047-789a-e54ae19c506b

To activate the partition and to be used immediately by the operating system, swapon is executed with the name of the partition as argument:

Swapon [device]

The following example switches to the / dev / sdb12 partition as swap partition :

Swapon / dev / sdb12

Run free to corroborate that new swap file is being used by the operating system. The output may look similar to the following:

             Total used free shared cached buffers
Mem: 321364 312576 8788 0 940 63428
- / + buffers / cache: 248208 73156
Swap: 1426416 0 1426416

For this partition to be used as swap memory automatically at the next system boot, the / etc / fstab file is edited :

I came / etc / fstab

The line to be added takes the following format:

[Partition] swap swap defaults 0 0

In the following example, the swap partition will be defined as the / dev / sdb12 partition :

/ Dev / sdb12 swap swap defaults 0 0

Assign more space to a logical volume.

Usually a good administrator will always leave unused space in a volume group. An advantage of using logical volumes is that they can be enlarged with the system in operation and no longer perform the process from a live disk. The following scenario assumes that this is the case and that the logical volume used as swap memory corresponds to / dev / mapper / lv_swap .

Turn off the swap memory partition:

Swapoff / dev / mapper / lv_swap

Run lvextend with the -l option using as + 100% FREE arguments and the device name of the logical volume to make this volume grow using 100% of the unused extensions in the volume group.

Lvextend -l + 100% FREE / dev / mapper / lv_swap

Activate the swap memory partition again:

Swapoff / dev / mapper / lv_swap

Use a file as swap memory.

This method is suitable for inexperienced users. The swap file can be in any file system path.

Run dd with if = / dev / zero , of = / swap ) and bs = 1024 and count = 512000. To create a file with zeros, named / swap and 524288000 bytes ( 512 MiB ):

Dd if = / dev / zero of = / swap bs = 1024 count = 512000

The output will be similar to the following:

512000 + 0 records read
512000 + 0 written records
524288000 bytes (524 MB) copied, 6.99465 s, 75.0 MB / s

Run mkswap with the -f option to force the formatting procedure, the -c sector check and / swap option as an argument to format this file:

Mkswap -f -c / swap

The output will be similar to the following:

Setting up swapspace version 1, size = 511996 KiB 
no label, UUID = fed2aba5-77c6-4780-9a78-4ae5e19c506b

To activate the partition and to be used immediately by the operating system, swapon is executed . In the following example, the swap file is activated as swap partition :

Swapon / swap

Run free to corroborate that there are 512 additional MiB of swap memory. The output may look similar to the following:

             Total used free shared cached buffers
Mem: 321364 312576 8788 0 940 63428
- / + buffers / cache: 248208 73156
Swap: 3145724 0 3145724

Or run swapon with the -s option :

Swapon -s

The above must return an output similar to the following:

Filename Type Size Used Priority
/ Dev / dm-2 partition 6336508 44 -1
/ Swap file 511996 0 20

To make the system permanently use this file as swap memory, edit the / etc / fstab file :

I came / etc / fstab

Add the following content:

/ Swap swap swap defaults 0 0

Optimization of exchange memory usage.

Vm.swappiness option.

The Linux kernel allows you to change how often applications and programs are moved from physical memory to swap memory through a value that defines the relative usage weight of the latter. This is controlled through the vm.swappiness parameter . The default value of this is 60. A value of 0 disables exchange memory usage altogether. A value of 100 would cause the system to use swap memory aggressively.

Run the following to corroborate the default:

Cat / proc / sys / vm / swappiness

Or run the following:

Sysctl vm.swappiness

The default value was set by Linux kernel developers to allow testing and diagnostics. It is a very high value for most of the uses that can be given to the operating system. This value should be changed to a lower value so that the system uses the swap memory less and it is preferable to reclaim the cache instead . This is a kind of static RAM (random access SRAM or S tatic R andom A ccess M emory).

In desktops systems it is recommended to use a value between 10 and 20 . A value between 20 and 60 can improve overall server performance.

The following example applies the value 10 for the / proc / sys / vm / swappiness file .

Echo 10> / proc / sys / vm / swappiness

Run sysctl with the -w option and vm.swappiness = 10 as the argument to do the same:

Sysctl -w vm.swappiness = 10

The above returns an output similar to the following and confirming that the change has been applied:

Vm.swappiness = 10

This change in the form variables immediately applies until the next system restart. To make the change permanent, edit the /etc/sysctl.conf file .

Vi /etc/sysctl.conf

Add the following line to the end of the file:

Vm.swappiness = 10

The above can be complemented by the option vm.vfs_cache_pressure , which will allow to release part of the cache memory. This controls the tendency for the kernel to reclaim the memory used to cache directories and inode objects. Its default value is 100 and specifies whether to attempt to reclaim directory paths (intesies) and inodes at a more or less fair rate with respect to the cache paging claim and exchange memory paging. Decreasing the value causes the kernel to prefer to retain the entries of directories and inodes in the cache. Increasing the value causes the kernel to prefer to reclaim the memory used by the directory and inode entries.

  • Increasing the value can improve some scenarios where little use is made of the file system as the case of desktops. It can worsen the performance of scenarios where the file system is actively used – usually servers – but there will be more free memory available and a lower probability of using the slow swap memory.
  • Decreasing the value can worsen some scenarios where little use is made of the file system as the case of desktops. It can improve the performance of scenarios where the file system is actively used – usually servers – where it may be appropriate to allocate more cache memory for the most used directory and inode entries at the expense of increased memory consumption.

The following example sets a value equal to 1000 for vm.vfs_cache_pressure :

Sysctl -w vm.vfs_cache_pressure = 1000

To make the change permanent, edit the /etc/sysctl.conf file .

Vi /etc/sysctl.conf

Add the following line to the end of the file:

Vm.vfs_cache_pressure = 1000

Exchange memory priority assignment.

It is possible to give more or less priority of use to the various files or devices for swapping memory to favor those housed in faster storage media – such as SDR units type SDRAM or devices created with zram. The value of the priority can be any integer between 0 and 65535. The higher the number, the higher the priority.

Disable the / swap file by executing the following:

Swapoff / swap

Activate swapon again with the -p option , the desired priority number between 0 and 65535 and / swap as argument. Example:

Swapon -p 30 / swap

Run swapon with the -s snip :

Swapon -s

The output will be similar to the following:

Filename Type Size Used Priority
/ Dev / dm-2 partition 6336508 44 -1
/ Swap file 511996 0 30

To make the change permanent, edit the / etc / fstab file :

Vi / etc / fstab

Add pri = 30 to the file or swap memory device options column. Example:

/ Swap swap swap defaults , pri = 30 	0 0

Restart the system and run the checks again by running swapon with the -s option .

Author

Am a tech geek.. Do you wanna know more about me..? My contents will do tell you.

Pin It