LinuxのRAMを制限するメモ

LinuxでRAMの少ない状態のテストをしたいなと思ってコマンドラインオプションにメモリ制限のオプションをつけて試してみました.

物理RAMモジュールが制限したい容量に刺し直すことができればいいのですが,都合の良いサイズのRAMモジュールがあるとは限りません.Linuxの起動オプションで制限できそうだったので試してみました.

Linuxマシンを起動して,GRUBのメニューで編集したいエントリを矢印上下キーで選択して, e ボタンを押して編集メニューに入ります.
linux /vmlinuz〜 といった行に移動して末尾に追加の起動オプションを追記します.今回はメモリを2GBに制限したいので, mem=2048m を指定します.(実メモリ以上にも設定できるようですがそこを使おうとしたときにクラッシュしそうですね)

linux /vmlinuz-5.13.2-mptcp+le9 root=/dev/mapper/yoga—​260—​vg-root ro
linux /vmlinuz-5.13.2-mptcp+le9 root=/dev/mapper/yoga—​260—​vg-root ro mem=2048m

Ctrl + x を押してこのオプションで起動します.

起動したら確認してみます.

$ cat /proc/cmdline (1)
BOOT_IMAGE=/vmlinuz-5.13.2-mptcp+le9 root=/dev/mapper/yoga--260--vg-root ro mem=2048m
$ head -1 /proc/meminfo (2)
MemTotal:        2032968 kB
$ free (3)
               total        used        free      shared  buff/cache   available
Mem:         2032968     1575032       70032      161360      387904      139588
Swap:              0           0           0
  1. コマンドラインオプションの確認
  2. メモリの確認
  3. メモリの確認

詳細はKernelドキュメントを

linux-5.13/Documentation/admin-guide/kernel-parameters.txt
        mem=nn[KMG]     [KNL,BOOT] Force usage of a specific amount of memory
                        Amount of memory to be used in cases as follows:

                        1 for test;
                        2 when the kernel is not able to see the whole system memory;
                        3 memory that lies after 'mem=' boundary is excluded from
                         the hypervisor, then assigned to KVM guests.

                        [X86] Work as limiting max address. Use together
                        with memmap= to avoid physical address space collisions.
                        Without memmap= PCI devices could be placed at addresses
                        belonging to unused RAM.

                        Note that this only takes effects during boot time since
                        in above case 3, memory may need be hot added after boot
                        if system memory of hypervisor is not sufficient.

今回の手順はアドホックな手順で再起動すれば元に戻ってしまいます.もし永続化したいのであれば, grub.cfg を書き換えればOKなはずです.今回のDebian Bullseye の場合は, /etc/grub.d/10_linux を編集して sudo update-grub2 の実行で /boot/grub/grub.cfg が更新されます.

環境
$ dpkg-query -W grub2* linux-image-`uname -r` procps
grub2
grub2-common    2.04-19
linux-image-5.13.2-mptcp+le9    5.13.2-mptcp+le9-3
procps  2:3.3.17-5
$ lsb_release -dr
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です