Raspberry Pi OSのswap(仮想メモリ)サービスの起動、停止、swapファイルサイズ変更等運用方法を掲載します。
Raspberry Pi OS Lite(64bit版) 2024-11-19 Raspberry Pi4で最新化しています。
現状の確認
普通にインストールすると、swap(仮想メモリ)サービスが起動します。
$ sudo swapon -s
Filename Type Size Used Priority
/var/swap file 204796 0 -2
$ sudo free -h
total used free shared buff/cache available
Mem: 3.7Gi 267Mi 3.3Gi 6.2Mi 195Mi 3.4Gi
Swap: 199Mi 0B 199Mi
/var/swap 204796、スワップファイルが約200MB確保されています。
Swap:199Mi なので仮想メモリとして200MBまで使用できます。
もしメモリが足らなくなると、200MBまでメモリを拡張してくれます。
swap(仮想メモリ)サービスの停止
swap(仮想メモリ)サービスを停止および非活性化する方法を説明します。
$ sudo swapoff --all
$ sudo systemctl stop dphys-swapfile
$ sudo systemctl disable dphys-swapfile
Synchronizing state of dphys-swapfile.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable dphys-swapfile
Removed /etc/systemd/system/multi-user.target.wants/dphys-swapfile.service.
サービスの状態を確認します。
$ systemctl status dphys-swapfile
○ dphys-swapfile.service - dphys-swapfile - set up, mount/unmount, and delete a swap file
Loaded: loaded (/lib/systemd/system/dphys-swapfile.service; disabled; preset: enabled)
Active: inactive (dead)
Docs: man:dphys-swapfile(8)
赤字の通りActive:inactive swap(仮想メモリ)サービスが停止しています。
下記のコマンドで、swap(仮想メモリ)の停止が確認できます。
$ sudo swapon -s
$ sudo sudo free -h
total used free shared buff/cache available
Mem: 3.7Gi 274Mi 3.2Gi 6.2Mi 259Mi 3.4Gi
Swap: 0B 0B 0B
赤字の通りswap:の横の数字が0となっているのでOKです。
これで、Raspberry Pi4を再起動してもswap(仮想メモリ)サービスは有効となりません。
swapファイルのサイズ変更
一般的にswapファイルのサイズは、サーバに搭載されているメモリと同じとするのが通説です。
搭載メモリが4GBのRaspberry Pi4の場合、4GBに拡張します。
前章の通りインストール直後のswapファイルは200MBしか確保されていません。もし200MBを超える仮想メモリが必要となった時、自動的にswapファイルが拡張されていくのですが、swapファイルを拡張するために更なる時間遅延が発生します。
swap(仮想メモリ)サービスを停止している状態で設定ファイルを変更します。
$ sudo vi /etc/dphys-swapfile
# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
# use under either modified/non-advertising BSD or GPL license
# this file is sourced with . so full normal sh syntax applies
# the default settings are added as commented out CONF_*=* lines
# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap
# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=4096
# set size to computed value, this times RAM size, dynamically adapts,
# guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2
# restrict size (computed and absolute!) to maximally this limit
# can be set to empty for no limit, but beware of filled partitions!
# this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
# but is also sensible on 64bit to prevent filling /var or even / partition
CONF_MAXSWAP=4096
1Gを超えるswapを設定する時は、CONF_MAXSWAPを有効にする必要があります。行頭に#が指定されている時は、削除して有効にしてください。
赤字の通り、4G(1024MB×4)に設定するため4096を設定します。
swap(仮想メモリ)サービスの起動
swap(仮想メモリ)サービスの活性化と起動します。
$ sudo systemctl enable dphys-swapfile
Synchronizing state of dphys-swapfile.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable dphys-swapfile
Created symlink /etc/systemd/system/multi-user.target.wants/dphys-swapfile.service → /lib/systemd/system/dphys-swapfile.service.
$ sudo systemctl start dphys-swapfile
実行の結果を確認します。
$ systemctl status dphys-swapfile
● dphys-swapfile.service - dphys-swapfile - set up, mount/unmount, and delete a swap file
Loaded: loaded (/lib/systemd/system/dphys-swapfile.service; enabled; preset: enabled)
Active: active (exited) since Sat 2025-02-08 13:57:26 JST; 11s ago
Docs: man:dphys-swapfile(8)
Process: 1273 ExecStart=/sbin/dphys-swapfile setup (code=exited, status=0/SUCCESS)
Process: 1316 ExecStart=/sbin/dphys-swapfile swapon (code=exited, status=0/SUCCESS)
Main PID: 1316 (code=exited, status=0/SUCCESS)
CPU: 143ms
前章で設定したswapファイルが作成されactiveになりました。
更に下記コマンド、設定どおり仮想メモリ4GBで起動している事を確認できます。
$ sudo swapon -s
Filename Type Size Used Priority
/var/swap file 4194300 0 -2
$ sudo sudo free -h
total used free shared buff/cache available
total used free shared buff/cache available
Mem: 3.7Gi 276Mi 3.2Gi 6.2Mi 266Mi 3.4Gi
Swap: 4.0Gi 0B 4.0Gi