swap for raspberry pi3

While building ffmpeg on my raspberry pi it just kept crashing during link phase with signal 9. This is mostly due to memory exhaustion. Now, I did not set a swap space on my rpi3 when it set it years ago.

To confirm the whether i had swap or not:

1
2
3
4
rpi3>sarang>~>free
              total        used        free      shared  buff/cache   available
Mem:         873332      144748      610944        3920      117640      706660
Swap:             0           0           0


create a swap file with 4GB memory. Seems like i have enough availabe already (check with sudo fdisk -l):

1
2
3
sudo fallocate -l 4g /mnt/4g.swap
sudo chmod 600 /mnt/4g.swap 
sudo mkswap /mnt/4g.swap


and here we go..

1
2
3
4
rpi3>sarang>~>free
              total        used        free      shared  buff/cache   available
Mem:         873332      144748      610944        3920      117640      706660
Swap:       4194300           0     4194300