First to determine the amount of available swap space, use the swap command:
# root@techgyaan:~#/usr/sbin/swap -l

The value in the free column indicates the number of free blocks, where a block is 512 bytes.
To convert blocks to KB, divide the number by 2. For example, 2,000,000 free blocks is equivalent to 1,000,000 KB.
If you do not have enough swap space, perform the following tasks:

1. Log on as the root user, and create an empty swap file:
# techgyaan$ su
Password: root_user_password)

root@techgyaan:~#mkfile 2048m /usr/swap
mkfile sizem swap_file_name)

Append an m to the size to specify the size in megabytes (example: 900m). The minimum size of this file must be greater than the difference between the currently available swap space and required swap space. For example, if you have 100 MB of free swap space, then this swap file must be at least 540 MB.

2. Add the file to the swap space using the following command:

root@techgyaan:~#/usr/sbin/swap -a /usr/swap
root@techgyaan:~#/usr/sbin/swap -a swap_file_name

3. Add a line to “/etc/vfstab” file. If you do not add the line, the new swap space does not persist when you restart the computer.
/path/to/swap/file – – swap – no –

Replace /path/to/swap/file with the location of the swap file.
4. Verify the new swap space size:
root@techgyaan:~#/usr/sbin/swap -l

Related:How do I add more swap space without formatting/deleting files on Ubuntu machine?