TransWikia.com

A linux program that uses memory

Super User Asked by tony_sid on December 4, 2020

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?

7 Answers

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: 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.

Correct answer by Sebastian on December 4, 2020

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).

Answered by Luc on December 4, 2020

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

Answered by user329881 on December 4, 2020

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

Answered by LawrenceC on December 4, 2020

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.

Answered by danthehat on December 4, 2020

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.

Answered by tylerl on December 4, 2020

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

Answered by Journeyman Geek on December 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP