Create Swap Virtual Memory on Centos 6 openVZ

Swap Virtual Memory help vps server with little amount of memory to add more amount of memory with Swap partition. For example, upgrade amount of RAM in system from 512 MB to 1 GB by create Swap partition 512 MB. Please note that not all vps hosting provider granted an VPS based on openVZ to create Swap Virtual Memory

Check physical memory usage, see physical memory free left only 34 MB, not good, vps server may stop working

#free -m
             total       used       free     shared    buffers     cached
Mem:           490        455         34          0         24         73
-/+ buffers/cache:        358        131
Swap:            0          0          0

Check disk space availability

#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1        9256088 1475508   7308836  17% /
tmpfs             250996       0    250996   0% /dev/shm

Add a swap file:
Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 512 MB swap file is 524288.
At a shell prompt as root, type the following command with count being equal to the desired block size: dd if=/dev/zero of=/swapfile bs=1024 count=524288

#dd if=/dev/zero of=/swapfile bs=1024 count=524288
524288+0 records in
524288+0 records out
524288824 bytes (512 MB) copied, 10.192 s, 105 MB/s
#mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 524288KiB
no label, UUID=d2592b02-8cb8-4892-8a5a-5af7f9654cc1
#swapon /swapfile
#swapon -s
Filename                                Type            Size    Used    Priority
/swapfile                               file            524288 0       -1
#vi /etc/fstab

# Paste this following line at bottom

/swapfile swap swap defaults 0 0

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:

#chown root:root /swapfile
#chmod 0600 /swapfile

Check the result!

#free -m
             total       used       free     shared    buffers     cached
Mem:           490        483          6          0          4        121
-/+ buffers/cache:        357        132
Swap:          511          0        511