Swapfile really needed?

probably you should have a look what tmpfs is and how it’s working

https://en.wikipedia.org/wiki/Tmpfs

great, so it is a RAMdisk basically. so when that 740 MB RAM space is only used to 1% there should be no need for a swap file, right?

tmpfs is a RAM disk yes, but free tmpfs space and free RAM space are not the same. tmpfs space can be as large as you want it, it doesn’t “know” how much RAM is actually available, it will only consume as much RAM as actual file space is used. But RAM of course is used by many other things as well, free and htop can be used to check free/available RAM. So even that the tmpfs shows you plenty of free space, RAM usage could be at its limit because of other processes or background services. Also tmpfs doesn’t know about swap, it will simply allocate additional memory as you write files inside. Swapping happens at a different layer, when the kernel recognises that RAM usage comes to its limit and there is some rare used data inside that can be written to disk without affecting performance much (due to low usage of those). On DietPi, we set swappiness=1 (in /etc/sysctl.d/dietpi.conf) which leads to swapping being done as little as possible. 0 would disable it completely, 60 is default on most distros. Using a higher number leads to swapping being done even to make space for file system caching already, so rarely used RAM data is written TO disk to make space for often used files FROM disk, so for performance reasons only. swappiness=1 doesn’t rule this out completely, but it will be done very rarely and it’s simply the best we can do to reduce disk writes to a minimum while preserving swap as emergency backup memory in case something is consuming unexpectedly much or for peak times, to prevent the “out-of-memory killer” from doing its job :wink:.

Ah as you asked, /tmp size on DietPi is set to 50% of total memory size (RAM + swap), AFAIK 50% RAM size is default on Debian. However as said its used/free space has nothing to do with actual RAM usage, there are even many other tmpfs mounts that all together provide a much larger “drive space” than actual memory is available :wink:. Those are simply used for different purpose and all of them have mostly a much larger size than what is ever required, just none of them should be alone larger than total memory.

thanks for that huge rundown on information about swap/tmp mechanism in dietpi!!! will sticky this for me and read it again and again.

Hello all,

I’m pulling out an old thread here.

  1. Does it make sense to move my swap files to the USB stick? My DietPi backups are on this stick.
  2. i have a Raspberry 1 Rev B with 512MB RAM for PiHole, Unbound and RPi-Monitor. Swap file also as 1GB sufficient?

I have enough memory on my two 16GB sticks. So it makes no sense to reduce the size of the sawp file, correct?

Thanks



your setup should be fine and usually there should not be a need to change anything. At the moment the SWAP file is not used. But as your system has 512MB RAM only, you should keep it just n case needed at a later stage.

But it’s generally a good idea to move the swap file to the USB stick, since SD cards are not made for surviving long with heavy writes. It practically doesn’t make a difference if the swap file isn’t used, but it may be used during maintenance tasks, software installs etc. 1 GiB seems sufficient as it’s currently not used, but it doesn’t hurt to leave it larger as long as USB drive space is sufficient.