search by tags

for the user

adventures into the land of the command line

adding swap space

swap space allows you to use some of the available hard drive space as RAM. its useful if you dont have much RAM, but you have a lot of physical storage

check current swap space and memory

$ free

total  used   free   shared buffers cached
Mem:   604480 294400 310080 208     16392   158420
-/+ buffers/cache: 119588 484892
Swap: 0 0 0

check available hard disk space

$ df -h

Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 9.9G 2.9G 6.5G  31%  /
tmpfs      296M 0    296M  0%   /dev/shm

decide how much hard disk space to use as memory swap space, in this case 2GB or 2048. use the ‘dd’ command to create an empty file

$ sudo dd if=/dev/zero of=/swap bs=1M count=2048

2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 60.4538 s, 35.5 MB/s

check the swap space has been created in the directory specified in the previous command

$ ls -l /swap

-rw-r--r--. 1 root root 2147483648 Nov 13 15:15 /swap

make the directory swap space

$ sudo mkswap /swap

mkswap: /swap: warning: don't erase bootbits sectors
 on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=a4758fad-8de0-402d-a829-7cd6617a874a

load the swap space and check memory again to see your shiny new swap space

$ sudo swapon /swap
$ free

     total  used   free  shared buffers cached
Mem: 604480 586520 17960 208    4308    450520
-/+ buffers/cache: 131692 472788
Swap: 2097148 0 2097148

make the swap file persistent by adding this line to the /etc/fstab file

$ /swap /swap swap defaults 0 0