1

I have a RaspberryPi 3B rev 1.2. It has 1 GB of RAM. When I run my C++ compiler at -j 2 or above I catch intermittent Out Of Memory (OOM) kills. Sometimes the compiler is killed, other times other programs are killed.

Raspbian Buster comes with dphys-swapfile and a 100 MB swap file in /var/swap. I'm not sure what dphys-swapfile does beyond a swap file in /var. The dphys-swapfile (8) man page does not state benefits of using the program.

Since the device is experiencing OOM kills with dphys-swapfile, it does not appear (to me) to solve any problems. dphys-swapfile also seems to forget to set swappiness to a sane value. The conf file /etc/dphys-swapfile lacks a setting for it.

What problem does dphys-swapfile solve?


On many ARM dev-board OSes I setup a small swap partition and set swappiness to a low value, like 3 or 5. That sidesteps the OOM kills and avoids my C++ inflicted DoS. The low swappiness also avoids most writes to the filesystem.

dphys-swapfile is complicating the fix. I'm not sure why it is not fixing the OOM kills, and I am not sure if I can safely remove it and fix things manually.

  • Thanks @goldilocks. I did not setup a swap partition (yet). I am still in the default Raspbian OS configuration. What I have noticed is, dphys-swapfile does not seem to increase the swapfile size it is managing. It also fails to adjust swappiness. It just keeps letting programs crash. – jww Dec 10 '19 at 15:45
  • 1
    In information systems where one studies the design and implementation of a system, I believe dphys-swapfile is known as a black hole process. Data/work flows into the box but nothing useful happens. There is no useful output. It is a useless process. – jww Dec 10 '19 at 15:48
  • I think you've misinterpreted the man page -- it doesn't do anything dynamic while the system is running; ie., there is no intention to deal with the issue you are talking about. I've added a chunk at the end of my answer with a bit of detail about that. – goldilocks Dec 10 '19 at 16:15
2

It's a system service. You can systemctl disable dphys-swapfile if you want, and list a swap partition in fstab instead (or whatever you normally do, or not use any swap). You can delete the swap file too since (pretty certain) its a dphys-swapfile exclusive thing.

I'm not sure why it is not fixing the OOM kills

Is your swap partition exactly the same size as the dphys-file? If not there's your answer. More swap = less OOM.

What problem does dphys-swapfile solve?

It's either a policy of Raspbian, the Debian armhf distro of which it is a repackaging, or Debian in general. In the context of the first two, it is a solution to not having a swap partition, which might be conceived of as preferrable because it is a little more user-proof and generally robust (since the file is is created and managed by the service, which could not be done with a swap partition).

And if it is a current policy of Debian in general (I guess you could check a new image about that), then the issue is probably the same: By using it as a default, there's no worry about having to involve the user (eg., when partitioning for a new install) with creating a swap partition.

It's also a more dynamic use of resources in an obvious sense, although since swap is usually only a few times RAM and storage much larger, on a normal desktop or server that would not be of much consequence. It might, though, in embedded or other contexts with much more limited storage (such as a Pi with a 16 GB card).

If you are a more experienced user with other preferences, it is easy enough to configure things differently.

Early versions of Raspbian used zram based swap (compressed memory), which by default took a 100 MB chunk of the 512 MB model 1 RAM. This was not a good choice, in my opinion, whereas the dphys-swapfile thing seems sensible enough.

What I have noticed is, dphys-swapfile does not seem to increase the swapfile size it is managing. It also fails to adjust swappiness. It just keeps letting programs crash

Ah, so the question in your title is a bit facetious ;) How effective this is I don't know, because with Pis I have pretty much always used a swap partition on the SD card, or no swap at all. However, off the cuff I think if the problem is something that occurs suddenly and occasionally (such as a big compile) then it likely can't or at least (arguably) shouldn't do anything too drastic. Most people probably do not want a service reconfiguring swap every few minutes.

But we may have gotten astray from the facts. It seems to me the major purpose of the service is to auto configure a swap space at boot without requiring a static partition. The man page says:

dphys-swapfile computes the size for an optimal swap file (and resizes an existing swap file if necessary), mounts an swap file, unmounts it, and deletes it it is not wanted any more

There is no claim that it dynamically resizes swap during runtime, or responds to system events such as excessive OOMs. Further, what "optimal" means here turns out to be the usual RAM ratio thing. /sbin/dphys-swapfile is a shell script and some of the comments probably better explain what it is intended to do:

      setup)
        # (re-)size/-generate, fast if no memory size change

        if [ "${CONF_SWAPSIZE}" = "" ] ; then
          # no absolute size given, so automatically compute optimal size
          echo -n "computing size, "
          # this seems to be the nearest to physical RAM size, lacks about 60k
          #   but it actually then fails from AMD64 kernel 2.6.32 onwards
          #KCORESIZE="`ls -l /proc/kcore | awk '{ print $5 }'`"
          ## make MBytes which rounded down will be exactly 1 too few, so add 1
          #MEMSIZE="`echo "${KCORESIZE} 1048576 / 1 + p" | dc`"
          # so second attempt at finding out physical RAM size, lacks about 10M
          #   see how long this variant stays usable :-)
          MEMTOTAL="`grep '^MemTotal:' /proc/meminfo | awk '{ print $2 }'`"
          # make MBytes which rounded down will be about 10 too few, so add 10
          MEMSIZE="`echo "${MEMTOTAL} 1024 / 10 + p" | dc`"
          # compute desired swap size, as factor * RAM
          CONF_SWAPSIZE="`echo "${MEMSIZE} ${CONF_SWAPFACTOR} * p" | dc`"
          # remove any fractional MBytes
          CONF_SWAPSIZE="`echo "${CONF_SWAPSIZE}" | cut -f 1 -d '.'`"
        fi

In case that's not clear (the echo to dc is a little arcane; dc does math, this clunky method is likely intended to be as portable as possible), it's getting the RAM size from /proc and applying a static multiplier.

It does not set swappiness at all.

There's no daemon or anything monitoring the system to see if swap should be reconfigured based on RAM usage metrics, etc. It uses either a user configured size, or a simple heuristic. While you could run it post boot to do the whole thing over, unless you change the configuration it will just do the same thing again.

So, a little disappointing but at least you know it isn't doing something more complicated badly.

| improve this answer | |
  • Thanks @godilocks. It looks like it is safe to remove dphys-swapfile. – jww Dec 10 '19 at 17:45
  • It's only safe to remove the swapfile if you will never over commit RAM. Swapping (which is much better on a USB device) is there to free up RAM from dormant processes to allow a memory hungry process to run. That's the sort of thing that's 100% invisible on your Windows laptop with 8GB or 16GB of RAM. On a Raspberry with 1GB of RAM having a swap file may save you from an unscheduled reboot. – Dougie Dec 10 '19 at 19:59
  • @Dougie A swap file has to be explicitly invoked (via swapon filepath); this is part of what the dphys-swapfile service does. If you have disabled dphys because you are using a different form of swap (eg., a partition listed in /etc/fstab), the swap file will never be spontaneously used regardless of circumstances. Whether there's much point in deleting it depends on how big it is in relation to how much space you have; if you never plan to re-enable dphys then it's just clutter. – goldilocks Dec 10 '19 at 20:34
  • For some (unknown) reason the RPF folks don't ship Raspbian with a swap partition. They have dphys-swapfile enabled (albeit the file size used to be too small to be any use). I don't think the kernel cares whether the swap space is in a partition or in a file container in the root. The point I was trying to make was that having 1GB of RAM may mean swapping is unavoidable. @goldilocks – Dougie Dec 10 '19 at 22:09
  • Thanks @Dougie. Yeah, I know I need a swapfile. After removing dphys-swapfile I manually added a partition with swapfile and set swappiness. I can run my compiler again :) – jww Dec 11 '19 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.