Ubuntu – Dynamic swap file like in Windows

14.04ssdswapusb-storage

I would like to install Ubuntu to a 8GB USB stick so really don't have space for swap. I have 8GB RAM and the most memory intense apps I use are Firefox and LibreOffice. I don't use hibernate. I know you can have a swap file in Linux but can you have one that grows and shrinks as needed? That would give me a safety net and most likely not make me loose any space with low swappiness. I researched the topic and found these steps:

sudo mkdir -p /var/cache/swap/

sudo fallocate -l 1G /var/cache/swap/swap0

or alternatively if the above fails:

sudo dd if=/dev/zero of=/var/cache/swap/swap0 bs=1M count=1024

Then:

sudo chmod 0600 /var/cache/swap/swap0

sudo mkswap /var/cache/swap/swap0

sudo swapon /var/cache/swap/swap0

The add this t /etc/fstab:

/var/cache/swap/swap0    none    swap    sw      0 0

Then install swapspace to make the swap file dynamic:

sudo apt-get install swapspace

And that is it? Just installing swapspace will decrease my swap file from 1GB to zero and let it grow up to 1GB?

Best Answer

Well you can try of course mentioned approaches. One more simple thing is to install Ubuntu just without swap partition at all. Linux will not block you from this, it can work without swap.