4
0

概要

特別なツールを使わずに、bashと簡単なコマンドでLinuxのメモリ使用量を意図的に増減させる

やり方

  1. 適当な容量のファイルを作成する
  2. メモリ使用量を増やすには、bashの変数に作成したファイルの中身を食べさせる
  3. メモリ使用量を減らすには、ファイルの中身を食べさせた変数をunsetする

具体的に

1. 適当な容量のファイルを作成する

ここでは100MBのダミーファイルを作っています
ファイルサイズはお好みに合わせて適当に調整すればよいです

100MBのファイルを作成する例
$ dd if=/dev/zero bs=1M count=100 | base64 | head -c 100M > 100m.data
$ ls -lh 100m.data
-rw-r--r--. 1 user1 user1 100M May  3 05:22 100m.data

2. メモリ使用量を100MB増やす

適当な変数に作成したファイルの中身を食べさせると、メモリ使用量が増える

増やす前(usedが326MB使用している)
$ free -m
               total        used        free      shared  buff/cache   available
Mem:            1775         326        1283           9         317        1448
Swap:           2083           0        2083
メモリ使用量を増やす
$ MEM1=$(cat 100m.data)
増やした後(usedがだいたい100MBぐらい増える)
$ free -m
               total        used        free      shared  buff/cache   available
Mem:            1775         406        1204           9         317        1369
Swap:           2083           0        2083

3. メモリ使用量をもっと増やす

増やしたい分だけの数の変数に、それぞれ最初作成したファイルの中身を食べさせます
ここでは、上のMEM1とあわせて合計5個の変数にそれぞれ100MBのファイルを食べさせたので、合計で500MBぐらいメモリ使用量が増えました

別の変数にも同じようにデータを食べさせる
$ MEM2=$(cat 100m.data)
さらにメモリ使用量が増えた
[user1@localhost ~]$ free -m
               total        used        free      shared  buff/cache   available
Mem:            1775         497        1112           9         317        1277
Swap:           2083           0        2083
どんどんデータを食べさせる
$ MEM3=$(cat 100m.data)
$ MEM4=$(cat 100m.data)
$ MEM5=$(cat 100m.data)
合計500MBぐらいメモリ使用量が増えた
$ free -m
               total        used        free      shared  buff/cache   available
Mem:            1775         804         806           9         317         971
Swap:           2083           0        2083

4. メモリ使用量を減らす

メモリ使用量を減らすには、その変数をunsetすればよいです

メモリ使用量を減らす
$ unset MEM1
$ unset MEM2
$ unset MEM3
$ unset MEM4
$ unset MEM5
減らした後(usedが増やす前と同程度に戻っている)
$ free -m
               total        used        free      shared  buff/cache   available
Mem:            1775         296        1314           9         317        1479
Swap:           2083           0        2083
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0