Out of RAM: Increase a meagre swapfile under Linux

Swap space is an area set aside for use in the event that physical RAM becomes full. On a Linux machine, when the system runs out of RAM inactive pages are moved to the Swap Space freeing up RAM.

Whilst building a Yocto Image on an old machine with limited RAM I was getting repeated failures during the compilation of qtbase. Analysis of the kernel log revealed that there was a shortage of memory, and hence a need to increase the swap space.

You can determine the current status of the swap space using the following command

$ sudo swapon --show
NAME      TYPE SIZE   USED PRIO
/swapfile file   2G 265.9M   -2

To increase the size of the swapfile, begin by disabling the swap space, and then remove the existing swapfile.

sudo swapoff /swapfile
sudo rm /swapfile

You can now create a new blank swapfile, set the permissions associated with it, and then re-enable the swap space.

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

ssh: How to Escape a Dead or Unresponsive Session

You can end a stuck ssh session by typing ~. at the beginning of a line. This means that if you have already been typing something you may have to hit the enter key before typing the ~ (tilde) and . (dot) characters.

The tilde character is an escape character in ssh. If you were to enter ~? at the beginning of a line you will get a list of supported escape sequences like the one seen below.

$ ~?
Supported escape sequences:
~. - terminate connection (and any multiplexed sessions)
~B - send a BREAK to the remote system
~C - open a command line
~R - request rekey
~V/v - decrease/increase verbosity (LogLevel)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

One useful item in the list is the suspend ssh sequence. If you enter ~ (tilde) followed by ^z ( ctrl and z) you will be returned to the command prompt on your local machine. You can then execute commands locally before returning to the ssh session using the fg command.