****************************************************
* Description: Knowledge of Linux memory swap space
* Date: 02:22 PM EST, 06/19/2017
****************************************************
<1> Linux Swap Space:
|
|__ o. Linux divides its physical RAM (random access memory) into chucks of memory called pages.
| Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory.
| The combined sizes of the physical memory and the swap space is the amount of virtual memory available.
|
|__ o. Swapping is necessary for two important reasons. First, when the system requires more memory than is physically available, the kernel swaps out less used pages
| and gives memory to the current application (process) that needs the memory immediately. Second, a significant number of the pages used by an application during
| its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications
| or even for the disk cache.
|
|__ o. However, swapping does have a downside. Compared to memory, disks are very slow. Memory speeds can be measured in nanoseconds, while disks are measured in milliseconds,
| so accessing the disk can be tens of thousands times slower than accessing physical memory. The more swapping that occurs, the slower your system will be.
| Sometimes excessive swapping or thrashing occurs where a page is swapped out and then very soon swapped in and then swapped out again and so on.
| In such situations the system is struggling to find free memory and keep applications running at the same time. In this case only adding more RAM will help.
|
|__ o. Linux has two forms of swap space: the swap partition and the swap file. The swap partition is an independent section of the hard disk used solely for swapping;
no other files can reside there. The swap file is a special file in the filesystem that resides amongst your system and data files.
<2> Checking swap size:
|
|__ $ swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 14434300 48116 -1 ......... Swap space on disk. Used unit is KB.
/mnt/resource/swapfile file 2097148 0 -1 ......... Swap space on file. Used unit is KB.
<3> Creating swap file:
|
|__ $ su - ............................................................................................... All the below actions should be performed as root.
|
|
|__ $ dd if=/dev/zero of=/mnt/resource/swapfile_1 bs=1024 count=5242880 .................................. To create a swap file.
| Read operations from /dev/zero return as many null characters (0x00)
| 5242880+0 records in as requested in the read operation.
| 5242880+0 records out
| 5368709120 bytes (5.4 GB) copied, 82.384 s, 65.2 MB/s
|
|
|__ $ mkswap /mnt/resource/swapfile_1 .................................................................... Mark this file as a swap file.
|
| mkswap: /swapfile: warning: don't erase bootbits sectors on whole disk. Use -f to force.
| Setting up swapspace version 1, size = 5242876 KiB
| no label, UUID=1b473708-e071-4a69-ab31-1334dd987c4d
|
|
|__ $ swapon /mnt/resource/swapfile_1 .................................................................... Mount it on the file system, and make it alive immediately.
|
|
|__ $ swapon -s .......................................................................................... Verify if swap file got created.
|
| Filename Type Size Used Priority
| /mnt/resource/swapfile file 2097148 0 -1
| /mnt/resource/swapfile_1 file 5242876 0 -2
|
|
|__ $ vi /etc/fstab ...................................................................................... Update FS table with newly added swap file.
/mnt/resource/swapfile_1 swap swap defaults 0 0
<4> Creating swap partition:
|
|__ $ sudo su -
|
|__ $ fdisk -l
|
|__ $ mkswap /dev/hdb1
|
|__ $ swapon /dev/hdb1
|
|__ $ swapon -s
|
|__ $ vi /etc/fstab
<5> Reference:
|
|__ Author: Gary Sims, Subject: All about Linux swap space, Source - https://www.linux.com/news/all-about-linux-swap-space
Your Comments