I need it execute a certain kind of linux program from the terminal. The only thing the linux program needs to do is use more and more memory. Are there any programs like that?

  • trying to do something like this? – James Mertz May 4 '11 at 5:18
  • 4
    Most poorly written programs will use more and more memory :) – ta.speot.is May 4 '11 at 5:47

From my previous answer on StackOverflow:

#include <stdlib.h>

int main() {
int *p;
while(1) {
    int inc=1024*1024*sizeof(char);
    p=(int*) calloc(1,inc);
    if(!p) break;


$ gcc memtest.c
$ ./a.out

upon running, this code fills up ones RAM until killed by the kernel. Using calloc instead of malloc to prevent "lazy evaluation". Ideas taken from this thread: https://stackoverflow.com/questions/4383059/malloc-memory-questions

This code quickly filled my RAM (4Gb) and then in about 2 minutes my 20Gb swap partition before it died. This works on 64bit Linux. If you use a 32bit OS, it will have a limit of 2 or 4GB before it dies.

| improve this answer | |

how about a fork bomb ?

else something that creates a series of ever larger ram disks? You can create a ram disk with

mkdir /tmp/ramdisk; chmod 777 /tmp/ramdisk

mount -t tmpfs -o size=256M tmpfs /tmp/ramdisk/

to create a 256 mb ram disk. you could probably script a script that will make a series of ram disks, and fill them repeatedly up until whatever you need would happen

| improve this answer | |
  • 2
    +1 for fork bomb - probably the most simple solution here. OP never mentioned monitoring the used memory. – new123456 May 4 '11 at 11:06
  • Fork bombs typically kill the system because of CPU constraints, not RAM. It will eventually fill up your RAM as well, though, if you want to go that route... – Luc Jan 12 '16 at 23:46

Save the following as mem.c, then run gcc -o mem mem.c, then run ./mem. It allocates 100MB of memory every second. It does nothing else. You can change the number of seconds its sleeps or how many MB it allocates by changing the numbers below.

#include <malloc.h>
#include <unistd.h>
#include <memory.h>
#define MB 1024 * 1024
int main() {
    while (1) {
        void *p = malloc( 100*MB );
        memset(p,0, 100*MB );

OK, tested and updated.

| improve this answer | |

There's a utility called stress which does this, among other things. apt-get install stress from Debian.

| improve this answer | |

eatmemory is a very simple program that we use to test low memory conditions on DB servers I Hope it works for you

| improve this answer | |

Most answers recommend compiling something; my approach was for an embedded device that already ran basic GNU tools and for which I could not compile something custom. I answered this here: https://unix.stackexchange.com/a/254976/30731

If you have basic GNU tools (head and tail) or BusyBox on Linux, you can do this to fill a certain amount of free memory:

</dev/zero head -c BYTES | tail

# Protip: use $((1024**3*7)) to calculate 7GiB easily
</dev/zero head -c $((1024**3*7)) | tail

This works because tail needs to keep the current line in memory, in case it turns out to be the last line. The line, read from /dev/zero which outputs only null bytes and no newlines, will be infinitely long, but is limited by head to BYTES bytes, thus tail will use only that much memory. For a more precise amount, you will need to check how much RAM head and tail itself use on your system and subtract that.

To just quickly run out of RAM completely, you can remove the limiting head part:

tail /dev/zero

If you want to also add a duration, this can be done quite easily in bash (will not work in sh):

cat <( </dev/zero head -c BYTES) <(sleep SECONDS) | tail

The <(command) thing seems to be little known but is often extremely useful, more info on it here: http://tldp.org/LDP/abs/html/process-sub.html

Then for the use of cat: cat will wait for inputs to complete until exiting, and by keeping one of the pipes open, it will keep tail alive.

If you have pv and want to slowly increase RAM use:

</dev/zero head -c BYTES | pv -L BYTES_PER_SEC | tail

For example:

</dev/zero head -c $((1024**3)) | pv -L $((1024**2)) | tail

Will use up to a gigabyte at a rate of a megabyte per second. As an added bonus, pv will show you the current rate of use and the total use so far. Of course this can also be done with previous variants:

</dev/zero head -c BYTES | pv | tail

Just inserting the | pv | part will show you the current status (throughput and total by default).

Credits to falstaff for contributing a variant that is even simpler and more broadly compatible (like with BusyBox).

| improve this answer | |
  • For the downvoter: I would love to hear any feedback to make my future posts more useful. – Luc Jan 14 '16 at 19:52
  • I like this very much. Here's my variant: function usefor() { grep -q . <(dd if=/dev/zero bs=$1 count=1 2>/dev/null; sleep $2); }. Then I can use units like this: usefor 512M infinity – Nick Russo Apr 3 '17 at 19:48

You could use mprime, which is, IIRC, a linux port of Prime95 of overclocking fame. mprime (and Prime95) will allow you to stress test the memory and CPU of your computer system. In addition, you can specify exactly how much memory you want mprime to use (to overflow into swap space, if you like).

Find mprime and prime95 here. I apologize in advance for the state of the website's navigation.

| improve this answer | |

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.