#! /bin/bash
#
# clear_ram.sh - Clear as much user-space ram as possible
# (until the OOM_killer gets us)
#
swapoff -a
mem=$(free -b | grep Mem | awk '{print $2}')
mount none -t tmpfs -o size=$mem /tmp
dd if=/dev/zero of=/tmp/zero.dat bs=1M &
echo "17" > /proc/$(pidof dd)/oomadj
while (pidof dd); do kill -USR1 $(pidof dd); done
this is a shell script.
what does this code do?
NOT HOMEWORK
This script
ddprocess to be first on the chopping block for the Out Of Memory killerddand its current status for as long as it keeps runningI say “attempts” because it should be writing to
oom_adjand notoomadj, at least for recent kernels, and because the max value is 15 and not 17.There’s also a bug here, because it will print the PID and status for all executing
dd, not just the one in the script.As the comment says, eventually the kernel Out Of Memory killer will kill the process.
I’m pretty sure it’s a silly thing to do. I don’t know of a reason why you would actually need to zero memory this way.