10

I'm testing my system with zram, I need a script that will use as much ram as possible. This script should fill up my ram with random stuff not just zeros.

5 Answers 5

11

Just run :

echo {1..1000000000}

Explanation :

The Shell, Before giving the command to the kernel, Expands all the regular expressions and short-hands. The expanded command is temporarily stored in RAM. The above command expands to a very large command and hence it will completely fill the RAM (tested on 8GB).

WARNING : This is not a controlled way of filling RAM. You might get stuck after running this command. I advice you to keep your system monitor open (for watching RAM usage) and try with smaller numbers.

4
  • 3
    This does not seem to work with a busybox echo (no regular expression support). I found this solution: tail /dev/zero
    – falstaff
    Aug 18, 2017 at 18:35
  • @falstaff tail /dev/zero is brilliant although it seems to crash firefox. Where did you learn about it? Your comment was linked here Nov 12, 2019 at 2:23
  • I guess its the OOM which kills firefox? Probably just because it uses quite some memory so that OOM feels its "the best" target... I do not remember where I learned about it or whether I came up with it myself.
    – falstaff
    Nov 13, 2019 at 8:11
  • Helped a lot while analyzing some JVM memory consumption issue. This memory was not claimed back after the process execution completed. Could claim it back only after killing the bash process (terminal) from where this command was executed. Nov 19, 2021 at 7:57
7

memtester is a user space program designed to allocate memory (any amount you specify) and test it with random patterns. It will avoid swap usage though. But if you take all memory away with memtester first (check with free -m) and then start anything else that uses a lot of memory (gimp, firefox, ...), that should get the swap going.

Another alternative would be something like openssl rand -base64 $((1024*1024*1024)) | less and in less use > to jump to the last line; this will cause 1GB of base64 encoded random data to be loaded in memory (but it's slow).

If you're looking for something more efficient, a small script in any scripting language (e.g. Python) might do.

#!/usr/bin/python2
import numpy
result = [numpy.random.bytes(1024*1024) for x in xrange(1024)]
print len(result)

That would allocate 1G of memory with random data and print the number of MB allocated before terminating. If you want more than 1024M, adapt the xrange value accordingly.

1
  • 1
    Super, just what I was looking for. Thank you for the quick answer.
    – Raansu
    May 28, 2013 at 12:33
6

As prophecy201 suggested, stress is a great tool to use up your system's memory. Adding more workers will use up more RAM, but it will also use up more CPU, which is pretty ineffecient if all you want is to test RAM. Not to mention that the CPU will be needed by zram for compression.

Instead, you should increase the amount of RAM that is used with the --vm-bytes flag. For example, to use up 4 GB of RAM with one worker:

stress -m 1 --vm-bytes 4G

You may also find the --vm-keep flag helpful as it will hold the memory allocation instead of continually reallocating, so the memory usage will be constant instead of fluctuating:

stress -m 1 --vm-bytes 4G --vm-keep

Lastly, take a look here to make sure zram is what you really want; since you do have swap, zswap may be a better solution: zram vs zswap vs zcache Ultimate guide: when to use which one

1
  • I find it easy to use. I used this to test if my Swap is working. sudo swapon -a Jul 20, 2017 at 14:59
4

I would suggest to use the program stress, installable from the repositories with sudo apt-get install stress.

To test your RAM use stress -m x where x is the number of workers which will fill up the ram. Choose more workers to use more RAM.

1

Expect zswap to work extremely badly---make performance much worse---if you fill memory with random bytes, especially if you randomly touch lots of memory in a hurry.

zram compresses the contents of pages of memory, and compression only works if the data are not random. Real data (especially in-memory data) is usually fairly compressible.

zram also only helps if you have "locality of reference", like most programs have---they tend to touch the same pages repeatedly before touching some other subset of pages. (This is why normal virtual memory works, too. Compressed caching is just adding a new level of memory between normal uncompressed pages and disk storage.)

If you know this, and are intentionally trying to test zswap under worst-case conditions, maybe to find bugs, go for it.

But if not, you should probably read Paul R. Wilson et al's paper "The Case for Compressed Caching in Virtual Memory systems," which explains when RAM compression helps, when it hurts, and how an adaptive algorithm can use in when it helps and not when it doesn't. (The paper is online in html on some USENIX site, and in pdf form somewhere else. Google it.)

Unfortunately, as far as I know zram does not do the kind of general automatic adaptation they describe, so you have to set the compressed cache size at some reasonable usual value for your workload.

One case where that will work well is if you have more RAM than any one of your programs uses, but tend to have idle applications using up memory too, and switch between apps. Idle apps' pages will tend to get compressed, and when you switch back to them, they only need to be uncompressed rather than loaded from disk.

You must log in to answer this question.

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