Linux command to create out of memory killer (OOM)

In linux ,when virtual memory(physical memory + swap) is used up ,OOM will kill one process to free memory.You can try below command to simulate an OOM

tail /dev/zero

After a while above command will use up all your virtual memory and been killed by OOM ,like below

user01@host01$ tail /dev/zero
Killed
user01@host01$

All you can get the relevant log messages via command dmesg

Out of memory: Kill process 15297 (tail) score 947 or sacrifice child
Killed process 15297, UID 1000001 (tail) total-vm:21404752kB, anon-rss:15804388kB, file-rss:4kB

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 “(“

Linux command to create 100% cpu loading

We can use below command to generate cpu loading in linux

yes > /dev/null &

Notes:

  • “yes” is a command to print “yes” to screen repeatedly
  • /dev/null is data sink ,which means the output of command “yes” will be discareded
  • “&” means let this command to be running in background
  • One above command will use up one cpu core (or Hyper Thread)

You can use command “cat /proc/cpuinfo” to check how many cpus you have, eg: if you have 4 cpus (probably 1 cpu with 2 cores and each core have 2 threads) , then you need to run 4 above command to make your cpu 100% busy

You can use below command to kill these commands

killall yes

Equivalent Solairs mkfile command in Linux

If you have Solaris administrator experience , you may know we can use mkfile to create a big empty file for some purpose.In linux there are some equivalent commands.

1 .truncate

For example ,we can use below command to make a 10G empty file

truncate -s 10G 10gfile

2. dd

Below command does the same thing as above truncate command

dd if=/dev/zero of=10gfile bs=1G seek=10 count=0

3. fallocate

Note: this command only been supported by some file systems like ext4 , xfs

fallocate -l 10G 10gfile