Use up all your memory on *nix

DISCLAIMER: I shall not be held responsible for any damage or loss of data resulting from your foolishness in running the following command.

I recently stumbled upon (and foolishly ran) a command that filled up all of my system’s memory:

$ yes $(yes)

It didn’t take very long for my memory to fill up. I tried ^C, ^\ and executing a $ killall yes and it failed to kill the process. When I tried to reboot, journalctl complained of being out of memory. I eventually had to reboot my system using the Magic SysRq key sequence REISUB (am I ever glad I have my Unicomp keyboard!)

So, why will this command fill up your memory?

Allow me to break down the function:

Indicates that this command should be run as a regular user (i.e. without root privileges). This isn’t actually part of the command and shouldn’t be included when typing it into the shell.
From the man page: “output a string repeatedly until killed”. By default, it outputs y.
This is a feature in bash which allows one to build commands using the output of another command. The command inside the () is executed and its output is inserted into the outer command. For example, $ mkdir "$(date)" becomes something like $ mkdir "Mon Feb 3 16:04:57 EST 2014" (if you haven’t it figured out, this creates a directory with the current date and time).

Have you figured out what it does? $(yes) is the bit that will fill up your memory. $() waits until the command has finished and will store the output in memory until it’s ready, but the problem is, yes will never finish until you kill it, thus a constant stream of y will be stored until your memory runs out. The outer yes command will never execute unless you find a way to terminate the inner yes process.

While doing the SysRq+E keystroke (send SIGTERM to all processes) of the SysRq sequence, I found out why I couldn’t kill the inner command with $ killall yes: the process I needed to kill was bash. bash is the process that handles the execution of $(), therefore bash was the one I needed to kill.

Don’t try this on a Mac; if you do, your only option may be to unplug your machine. Holding down the power button may work, or maybe OS X will try to terminate Terminal, but I wouldn’t count on it.

  1. aa said:

    see you have an ad after your post. Is there a way to use that geek command you posted about to generate an infinite ad revenue loop? hehe

    • That’s an interesting thought.
      Actually, I’d rather not have ads, but is free and they have to make money somehow to keep it that way.

`$name' says...

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: