Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
trying to do something like this? – KronoS May 4 '11 at 5:18
3  
Most poorly written programs will use more and more memory :) – ta.speot.is May 4 '11 at 5:47
up vote 8 down vote accepted

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;
    }
}

run

$ 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: http://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.

share|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

share|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 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 );
        sleep(1);
    }
}

EDIT
OK, tested and updated.

share|improve this answer

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

share|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

share|improve this answer

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.

share|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: http://unix.stackexchange.com/a/254976/30731

If you have basic GNU tools (sh, grep, yes and head) you can do this:

yes | tr \\n x | head -c $BYTES | grep n
 # Protip: use `head -c $((1024*1024*2))` to calculate 2MB easily

This works because grep loads the entire line of data in RAM (I learned this in a rather unfortunate way when grepping a disk image). The line, generated by yes, replacing newlines, will be infinitely long, but is limited by head to $BYTES bytes, thus grep will load $BYTES in memory. Grep itself uses like 100-200KB for me, you might need to subtract that for a more precise amount.

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

cat <(yes | tr \\n x | head -c $BYTES) <(sleep $SECONDS) | grep n

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 grep alive.

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

yes | tr \\n x | head -c $BYTES | pv -L $BYTESPERSEC | grep n

For example:

yes | tr \\n x | head -c $((1024*1024*1024)) | pv -L $((1024*1024)) | grep n

Will use up to a gigabyte at a rate of 1MB 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:

yes | tr \\n x | head -c $BYTES | pv | grep n

Just inserting the | pv | part will show you the current status (throughput and total, by default, I think - otherwise see the man(ual) page).

share|improve this answer
    
For the downvoter: I would love to hear any feedback to make my future posts more useful. – Luc Jan 14 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .