How to allocate system memory for test purpose in Linux ?

Suppose you want to test system behavior when memory usage reaches 90% or more.

Hence you want a mechanism to increase the used system memory to this level.

Now you will start several programs which consume more memory and test the scenario.

However in Linux, we can use a small hack by dd command to increase the memory usage.

Here comes the trick…

1. Lets check the current memory usage of user saket.

[root@nglinux ~]# user=saket; echo -e "\n############ \n Memory usage of user $user \n RSS \t VSZ"; ps -U $user -o rss,vsz --no-headers | awk '{rss+=$1; vmem+=$2} END{print rss" "vmem}' | awk '{rss2=$1/1024; vmem2=$2/1024} END{print " "rss2" "vmem2}'; echo -e "#############";

############ 
 Memory usage of user saket 
 RSS 	 VSZ
 3.18359 10.0234
#############

Hence the current memory usage by user saket is 10 MB.

2. Now lets run the dd command to increase the memory usage by 200MB.

[saket@nglinux ~]$ dd if=/dev/zero of=/dev/null bs=200M &
^C89+1 records in
89+0 records out
18664652800 bytes (19 GB) copied, 8.76662 s, 2.1 GB/s

3. Now go to another terminal, and check the memory usage again.

[root@nglinux ~]# user=saket; echo -e "\n############ \n Memory usage of user $user \n RSS \t VSZ"; ps -U $user -o rss,vsz --no-headers | awk '{rss+=$1; vmem+=$2} END{print rss" "vmem}' | awk '{rss2=$1/1024; vmem2=$2/1024} END{print " "rss2" "vmem2}'; echo -e "#############";

############ 
 Memory usage of user saket 
 RSS 	 VSZ
 203.773 214.176
#############
[root@nglinux ~]# 

Hence we can see the memory usage is increased by 200MB since dd command allocates the block size from system virtual memory and then copies it to destination.

In case you want to occupy 1TB then you can increase the block size to 1TB.

I hope you liked the trick.

Do subscribe to this blog to stay updated with Linux/Unix articles.

Leave a Reply

Your email address will not be published.