A maioria das respostas recomenda compilar algo; minha abordagem era para um dispositivo embutido que já executava ferramentas GNU básicas e para as quais eu não conseguia compilar algo personalizado. Eu respondi isso aqui: link
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).