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?
|
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 |
|||||||||||||||||||||
|
|
stress is a workload generator that simulates cpu/mem/io/hdd stress on POSIX systems. This call should do the trick on Linux:
Adapt the |
|||||
|
|
You can write a C program to Then just let the program wait for keyboard input, and unlock the memory, free the memory and exit. |
|||||||||||||||||||||
|
|
I would suggest running a VM with limited memory and testing the software in that would be a more efficient test than trying to fill memory on the host machine. That method also has the advantage that if the low memory situation causes OOM errors elsewhere and hangs the whole OS, you only hang the VM you are testing in not your machine that you might have other useful processes running on. Also if your testing is not CPU or IO intensive, you could concurrently run instances of the tests on a family of VMs with a variety of low memory sizes. |
|||
|
|
(look in linux/Documentation/kernel-parameters.txt for details). |
||||
|
|
|
From this HN comment: https://news.ycombinator.com/item?id=6695581
|
|||||||||||||
|
|
How abount a simple python solution?
|
|||||||||||||||||
|
|
I keep a function to do something similar in my dotfiles. https://github.com/sagotsky/.dotfiles/blob/master/.functions#L248
|
|||
|
|
|
If you have basic GNU tools (
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 If you want to also add a time constraint, this can be done quite easily in
The Then for the use of If you have
For example:
Will use up to a gigabyte at a rate of 1MB per second. As an added bonus,
Just inserting the Why another answer? The accepted answer recommends installing a package (I bet there's a release for every chipset without needing a package manager); the top voted answer recommends compiling a C program (I did not have a compiler or toolchain installed to compile for your target platform); the second top voted answer recommends running the application in a VM (yeah let me just dd this phone's internal sdcard over usb or something and create a virtualbox image); the third suggests modifying something in the boot sequence which does not fill the RAM as desired; the fourth only works in so far as the /dev/shm mountpoint (1) exists and (2) is large (remounting needs root); the fifth combines many of the above without sample code; the sixth is a great answer but I did not see this answer before coming up with my own approach, so I thought I'd add my own, also because it's shorter to remember or type over if you don't see that the memblob line is actually the crux of the matter; the seventh again does not answer the question (uses ulimit to limit a process instead); the eighth tries to get you to install python; the ninth thinks we're all very uncreative and finally the tenth wrote his own C++ program which causes the same issue as the top voted answer. |
|||||||||||||||||||||
|
|
How about ramfs if it exists? Mount it and copy over a large file?
If there's no |
||||
|
|
|
If you want to test a particular process with limited memory you might be better off using ulimit to restrict the amount of allocatable memory. |
|||||
|
|
I think this is a case of asking the wrong question and sanity being drowned out by people competing for the most creative answer. If you only need to simulate OOM conditions, you don't need to fill memory. Just use a custom allocator and have it fail after a certain number of allocations. This approach seems to work well enough for SQLite. |
|||
|
|
|
I wrote this little C++ program for that: https://github.com/rmetzger/dynamic-ballooner The advantage of this implementation is that is periodically checks if it needs to free or re-allocate memory. |
|||
|
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?