I have a little low-end POC (proof of concept) I’m testing out at home before I bring it in to work. The POC requires my Raspberry Pi to run a couple of Docker containers which isn’t really a problem. However, I’m using a 1st-gen Pi that came out with only 512MB RAM total. By the time Docker itself was up and running, I had something on the order of 40-50MB of RAM left and another 99MB swap with the stock swap settings.
me@pinode:~ $ free -gh total used free shared buffers cached Mem: 434M 387M 46M 4.8M 66M 244M -/+ buffers/cache: 77M 356M Swap: 99M 0B 99M
And that’s after uninstalling X-server and all the GUI cruft that’s not needed for my POC. That’s just not gonna cut it, hoss…
My Pi was going to need a memory boost but a physical RAM upgrade was obviously out of the question while the default setting of 100MB swap would be just enough to cause unstable behavior post-Docker-container setup, so let’s get some extra swap space in for the rescue.
1) Turn swap off pre-“upgrade” – make sure beforehand you’re not running any processes that’ll bonk your system with the sudden loss of memory. Also, make sure you have enough free space remaining on your SD card or external drive:
me@pinode:~ $ sudo dphys-swapfile swapoff
2) Open the pre-existing swapfile with your favorite text editor:
me@pinode:~ $ sudo vim /etc/dphys-swapfile
3) Increase CONF_SWAPSIZE to your desired value – I wanted to add a full 1GB of swap. Uncomment the line if it’s commented:
4) Write the file to disk, exit your text editor, then attempt to turn the swap back on:
me@pinode:~ $ sudo dphys-swapfile swapon
5) Check your free memory – it will likely take some time to show up so let’s do it with a watch:
me@pinode:~ $ watch free -gh Every 2.0s: free -gh total used free shared buffers cached Mem: 434M 390M 44M 4.8M 66M 246M -/+ buffers/cache: 77M 356M Swap: 99M 0B 99M
6) If after a minute or two the swap size doesn’t jump up to the value you entered in /etc/dphys-swapfile, Ctrl+C to exit the watch, reboot your Pi w/ sudo reboot and log back in.
7) Once the Pi has rebooted and you’ve logged back in, try watch free -gh again for a couple more minutes. The Pi should eventually indicate that the swap size has been increased. Then, on to the next part of your project!
me@pinode:~ $ watch free -gh Every 2.0s: free -gh total used free shared buffers cached Mem: 434M 403M 30M 4.5M 14M 332M -/+ buffers/cache: 56M 377M Swap: 1.0G 0B 1.0G
Note: This is a stop-gap measure – even with an SD card or SSD external drive, swap space acting as RAM is much, much slower than physical RAM. However, this may not matter for your requirements, so have at it!