2014-07-28

Create swap file or virtual memory without partition

In my experience, Linux is an OS that quite require much RAM, especially for running software that was should be executed using Virtual Machines. When our programs using more than available memory, the OS will be unresponsive, to solve those problem, we could virtually add more RAM using disk. Here's some example to add 8GB swapfile to our system:

# create new file on "/swapfile" with size 1MB * 8192 = 8GB
sudo dd if=/dev/zero of=/swapfile bs=1048576 count=8192

# format the newly created file as swap and give correct permissions
sudo mkswap /swapfile
sudo chmod 0600 /swapfile

# enable swap temporarily (swap will be gone when computer restart)
sudo swapon /swapfile

# add configuration on fstab to enable swap on boot
echo /swapfile swap swap defaults 0 0 | sudo tee -a /etc/fstab

to check swap usage, use free -m command.