swap space is a dedicated device or a file in one of the mounted file systems to which the system swaps pages of information from the physical memory onto these areas to allow more current application use the physical memory when the physical memory is running out of space.
Let us see here how we can add a file within the File System as a swap area either temporarily to troubleshoot a performance issue or permanently (atleast until you add additional physical memory) to your system in SuSE and openSUSE.
First to start with, lets see how much of total memory we have on the system. This is the sum of Physical memory and any swap space already added to the system using the “free” command:
opensuse11:~ # free -tom
total used free shared buffers cached
Mem: 998 973 24 0 20 795
Swap: 739 0 739
Total: 1738 973 764
Here we have 739MB of swap and none of which is used at the moment. To see the swap device or file which provides this 739MB
opensuse11:~ # swapon -s
Filename Type Size Used Priority
/dev/mapper/system-swap partition 757752 0 -1
/swap_1 file 999992 0 -2
Now, lets say the system is really stressed and is running out of memory (ofcourse not the case here on my system) and we decide to quickly add a big 1Gigabyte file as a temporary swap area. We need to first create thios big monstreous 1Gigabyte file.
opensuse11:~# dd if=/dev/zero of=/swap_1 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 28.7256 s, 35.6 MB/s
We use the DD command to create the 1G file basically writing “0” into the file swap_1 in the “/” file system.
Now, the file is ready and we shall make it a swap area.
opensuse11:~ # mkswap /swap_1
Setting up swapspace version 1, size = 999996 KiB
no label, UUID=4aada58e-d6f7-4053-adf1-8d4c79122d36
That sets up the file as a swap area.
Now, we’ll simply enable the swapfile so that the system can start paging physical memory pages onto it.
opensuse11:~ # swapon /swap_1
Thats it. The system should now have an additional 1Gigabyte of swap area. Let’s see if it is true.
opensuse11:~ # free -tom
total used free shared buffers cached
Mem: 998 973 24 0 20 795
Swap: 1716 0 1716
Total: 2714 973 1741
It is…We now have 1.7Gigabytes of swap area as against to 739MB we originally had.
To confirm the swap areas we have
opensuse11:~ # swapon -s
Filename Type Size Used Priority
/dev/mapper/system-swap partition 757752 0 -1
/swap_1 file 999992 0 -2
While this provides the swap space required for the system almost instantly, if you happen to reboot your SuSE server or your openSUSE system then this swap space won’t be available for the system. To make it permanent, we need to add an entry to the /etc/fstab file as follows:
/swap_1 swap swap defaults 0 0
Where /swap_1 is our new swap file and it instructs the system to mount it at system startup as a swap area.
Thanks for the tip.
Diego (from Argentina)