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.