How to fill 90% of the free memory?

  • I want to do some low-resources testing and for that I need to have 90% of the free memory full.

    How can I do this on a *nix system?

    Does it really have to work on *any* *nix system?

    Instead of jut filling memory, could you instead create a VM (using docker, or vagrant, or something similar) that has a limited amount of memory?

    @abendigo For a QA many of the solutions presented here are useful: for a general purpose OS without a specific platform the VM or kernel boot parameters could be useful, but for a embedded system where you know the memory specification of the targeted system I would go for the filling of the free memory.

  • tkrennwa

    tkrennwa Correct answer

    7 years ago

    stress-ng is a workload generator that simulates cpu/mem/io/hdd stress on POSIX systems. This call should do the trick on Linux < 3.14:

    stress-ng --vm-bytes $(awk '/MemFree/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1
    

    For Linux >= 3.14, you may use MemAvailable instead to estimate available memory for new processes without swapping:

    stress-ng --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1
    

    Adapt the /proc/meminfo call with free(1)/vm_stat(1)/etc. if you need it portable.

    stress --vm-bytes $(awk '/MemFree/{printf "%d\n", $2 * 0.097;}' < /proc/meminfo)k --vm-keep -m 10

    Most of MemFree is kept by OS, so I used MemAvailable instead. This gave me 92% usage on Cent OS 7. `stress --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.98;}' < /proc/meminfo)k --vm-keep -m 1`

    Just as an added note, providing both `--vm 1 and --vm-keep` are very important. Simply `--vm-bytes` does nothing and you might be misled into think you can allocate as much memory as you need/want. I got bit by this until I tried to sanity check myself by allocation 256G of memory. This is not a flaw in the answer, it provides the correct flags, just an additional caution.

    This is why there is `-m 1`. According to the stress manpage, `-m N` is short for `--vm N`: spawn `N` workers spinning on `malloc()/free()`

    I get an allocation error: `stress-ng --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1` `stress-ng: info: [28129] defaulting to a 86400 second (1 day, 0.00 secs) run per stressor` `stress-ng: info: [28129] dispatching hogs: 1 vm` `stress-ng: error: [28148] stress-ng-vm: gave up trying to mmap, no available memory` `stress-ng: info: [28129] successful run completed in 10.02s` I also notice frequent crashes due to alloc errors. What can it be?

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM