swap(仮想メモリ)の運用方法(trixie版)

swap(仮想メモリ)の運用方法(trixie版)
swap(仮想メモリ)の運用方法(trixie版)

trixie(Debian 13系)では、従来のdphys-swapfileではなくzramベースのswap(圧縮RAMスワップ)が標準になりました。rpi-swap パッケージで管理され、設定は systemd と連携して自動生成されます。

bookworm(Debian 12系)以前のswapの運用は、下記の記事を参考にしてください。

現在のswap(仮想メモリ)の状態確認

Raspberry Pi5(8Gメモリ)の場合、初期状態は下記の通り、zramで2Gが確保されています。

# swapユニット(systemd)を一覧で確認
$ systemctl list-units --type=swap
  UNIT           LOAD   ACTIVE SUB    DESCRIPTION                             
  dev-zram0.swap loaded active active rpi-swap managed swap device (zram+file)

Legend: LOAD   → Reflects whether the unit definition was properly loaded.
        ACTIVE → The high-level unit activation state, i.e. generalization of SUB.
        SUB    → The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

# 有効なswapデバイスの詳細
$ swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   2G   0B  100


# メモリとswapの概況
free -h
               total        used        free      shared  buff/cache   available
Mem:           7.9Gi       562Mi       6.9Gi       107Mi       598Mi       7.3Gi
Swap:          2.0Gi          0B       2.0Gi

設定の前に、まず準備

公式の/etc/rpi/swap.confはそのまま残し、ドロップインという方式で設定を行います。

目的は、大元の/etc/rpi/swap.confがアップデートで変わっても、設定内容は、/etc/rpi/swap.conf.d/swap.confに維持するためです。

  1. /etc/rpi/swap.conf.dのフォルダを作成
    $ sudo /etc/rpi/swap.conf.d
  2. *.confのファイルを作成
    $ sudo cp /etc/rpi/swap.conf /etc/rpi/swap.conf.d
  3. *.confに変更部分だけ設定
    $ sudo vi /etc/rpi/swap.conf.d/swap.conf

インストール直後のswap.confは下記のとおりです。赤字は、説明を加筆した部分です。

#  This file is part of rpi-swap.
#
#  Defaults are provided as commented-out options. Local configuration
#  should be created by either modifying this file, or by creating "drop-ins" in
#  the swap.conf.d/ subdirectory. The latter is generally recommended.
#
#  See swap.conf(5) for details.

[Main]
#Mechanism=auto    # auto/none/zram/hybrid/file

[File]
#Path=/var/swap
#RamMultiplier=1      # RAMの何%をzramに割り当てるか(例25%=>0.25)
#MaxSizeMiB=2048      # 上限MB
#MaxDiskPercent=50    # ディスクサイズの上限の%
#FixedSizeMiB=        # 固定のファイルサイズMB
#Priority=60          # 値が大きいほど優先度が高い。既定60

[Zram]
#RamMultiplier=1      # RAMの何%をzramに割り当てるか(例25%=>0.25)
#MaxSizeMiB=2048      # 上限MB
#FixedSizeMiB=        # 固定MB
# Writeback settings (for zram+file mechanism):
#WritebackTrigger=auto          # zram使用率が80%を超えたら退避開始の場合 80% 指定
#WritebackInitialDelay=180min   # システム起動後やswapサービス開始後に、最初のwritebackを実行するまでの待機時間
#WritebackPeriodicInterval=24h  # zramのアイドルページをファイルスワップに書き戻す処理(writeback)を定期的に繰り返す間隔
#CompAlgorithm=zstd            # 圧縮アルゴリズムの指定例(省略可。既定はzstd系が多い)
#Priority=100                  # 値が大きいほど優先度が高い。既定100
#EnableWriteback=false         # writeback(アイドルページを/var/swapへ24時間毎に退避するハイブリッド挙動)を無効化したい場合:

swapを全く使用しない場合の設定

swapの機能を使用しない場合は、下記の赤字の部分の設定を行います。

$ sudo vi /etc/rpi/swap.conf.d/swap.conf
#  This file is part of rpi-swap.
#
#  Defaults are provided as commented-out options. Local configuration
#  should be created by either modifying this file, or by creating "drop-ins" in
#  the swap.conf.d/ subdirectory. The latter is generally recommended.
#
#  See swap.conf(5) for details.

[Main]
Mechanism=none

[File]
#Path=/var/swap
#RamMultiplier=1
#MaxSizeMiB=2048
#MaxDiskPercent=50
#FixedSizeMiB=

[Zram]
#RamMultiplier=1
#MaxSizeMiB=2048
#FixedSizeMiB=
# Writeback settings (for zram+file mechanism):
#WritebackTrigger=auto
#WritebackInitialDelay=180min
#WritebackPeriodicInterval=24h

再起動します。

$ sudo reboot

再起動後の状況を確認します。

# swapユニット(systemd)を一覧で確認
$ systemctl list-units --type=swap
  UNIT LOAD ACTIVE SUB DESCRIPTION

0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

# 有効なswapデバイスの詳細(何も表示されません。)
$ swapon --show

# メモリとswapの概況
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.9Gi       452Mi       7.1Gi        66Mi       430Mi       7.4Gi
Swap:             0B          0B          0B

swapにメモリ(2GB)のみ使用する場合の設定

Raspberry Pi5は、メモリに余裕があるので、下記設定を使用しています。

$ sudo vi /etc/rpi/swap.conf.d/swap.conf
#  This file is part of rpi-swap.
#
#  Defaults are provided as commented-out options. Local configuration
#  should be created by either modifying this file, or by creating "drop-ins" in
#  the swap.conf.d/ subdirectory. The latter is generally recommended.
#
#  See swap.conf(5) for details.

[Main]
Mechanism=zram

[File]
#Path=/var/swap
#RamMultiplier=1
#MaxSizeMiB=2048
#MaxDiskPercent=50
#FixedSizeMiB=

[Zram]
#RamMultiplier=1
MaxSizeMiB=2048
FixedSizeMiB=2048
# Writeback settings (for zram+file mechanism):
#WritebackTrigger=auto
#WritebackInitialDelay=180min
#WritebackPeriodicInterval=24h

再起動します。

$ sudo reboot

再起動後の状況を確認します。

# swapユニット(systemd)を一覧で確認
$ systemctl list-units --type=swap
  UNIT           LOAD   ACTIVE SUB    DESCRIPTION                        
  dev-zram0.swap loaded active active rpi-swap managed swap device (zram)

Legend: LOAD   → Reflects whether the unit definition was properly loaded.
        ACTIVE → The high-level unit activation state, i.e. generalization of SUB.
        SUB    → The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

# 有効なswapデバイスの詳細
$ swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   2G   0B  100

# メモリとswapの概況
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.9Gi       466Mi       7.1Gi        58Mi       413Mi       7.4Gi
Swap:          2.0Gi          0B       2.0Gi

swapにメモリ(1GB)とディスク(2GB)を併用する場合の設定

この方法は只今トライ中で、まだエラーが出て正しく設定ができいません。ご注意ください

持っているRaspberry Pi4は4GBなので、メモリに余裕がないので、zramに1GBをSSDに2GBを割り当てます。zramを優先で使うように設定しています。

$ sudo vi /etc/rpi/swap.conf.d/swap.conf
#  This file is part of rpi-swap.
#
#  Defaults are provided as commented-out options. Local configuration
#  should be created by either modifying this file, or by creating "drop-ins" in
#  the swap.conf.d/ subdirectory. The latter is generally recommended.
#
#  See swap.conf(5) for details.

[Main]
Mechanism=hybrid

[File]
Path=/var/swap
#RamMultiplier=1
MaxSizeMiB=2048
#MaxDiskPercent=50
FixedSizeMiB=2048
Priority=60

[Zram]
#RamMultiplier=1
MaxSizeMiB=1024
FixedSizeMiB=1024
Priority=100
# Writeback settings (for zram+file mechanism):
EnableWriteback=true              # アイドルページをファイルに退避
WritebackTrigger=80%              # zram使用率が80%を超えたら退避開始
WritebackInitialDelay=10min       # 起動後10分待ってからwriteback開始
WritebackPeriodicInterval=24h     # 24時間ごとに定期的にwritebackを実行

再起動します。

$ sudo reboot

再起動後の状況を確認します。

# swapユニット(systemd)を一覧で確認
$ systemctl list-units --type=swap
  UNIT           LOAD   ACTIVE SUB    DESCRIPTION                             
  dev-zram0.swap loaded active active rpi-swap managed swap device (zram+file)

Legend: LOAD   → Reflects whether the unit definition was properly loaded.
        ACTIVE → The high-level unit activation state, i.e. generalization of SUB.
        SUB    → The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.


# 有効なswapデバイスの詳細
$ swapon --show
NAME       TYPE       SIZE USED PRIO
/dev/zram0 partition 1024M   0B  100

# メモリとswapの概況
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.9Gi       387Mi       7.2Gi        18Mi       343Mi       7.5Gi
Swap:          1.0Gi          0B       1.0Gi

# swapファイルの確認
$ ls -al /var/swap
-rw------- 1 root root 2147483648 Nov 28 15:23 /var/swap
タイトルとURLをコピーしました