Shell command to create physical memory usage

No matter for what reason ,we as admin sometimes want to simulate memory usage . There have many ways to do this ,below one is to use shell command ,so we don’t need additional tools or code by specific program language.

yes |tr \\n a |head -c $((1024*1024*100)) | grep x

Above command will create 100M memory usage . 1024*1024 means M , and 1024*1024*1024 means G , you can specify what you want

In case you want to simulate use up all physical memory you can specify a number which is bigger than your total physical memory.

If you wan to let this occurs for period , you can try below command.

cat <(yes |tr \\n a |head -c $((1024*1024*1024*2)) ) <(sleep 100) |grep x

This command will simulate occupying 2G physical memory for 100 seconds.

Note: There is no SPACE between the “<” and “(“