本記事は、bookworm(Debian12)時代のdphys-swapfile時代のものなので、trixieからはrpi-swapに置き換えられたので参考になりません。ご注意ください。
本番で動くRaspberry Pi4のメモリは、各種チューニングでそれなりにメモリを使用します。
負荷が高い状態で(幸せな状態ですが)、保守作業をしても更なる遅延が発生しないようswapファイルを拡張します。
WordPress専用のサーバとして使用する場合、スペックを超える沢山のアクセスには、接続拒否をしてメモリの使い過ぎを押さえる事も可能です。swap(仮想メモリ)サービスの停止は、下記の記事を参照してください。
現在のswap(仮想メモリ)の状態確認
普通にインストールすると、swap(仮想メモリ)サービスは起動しています。
$ sudo swapon -s
Filename                                Type            Size    Used    Priority
/var/swap                              file            102396  0       -2
$ sudo free -h
               total        used        free      shared  buff/cache   available
Mem:           3.7Gi       173Mi       3.3Gi       6.0Mi       211Mi       3.5Gi
Swap:           99Mi          0B        99Mi/var/swap 102396、スワップファイルが約100MB確保されています。
Swap:99Mi なので仮想メモリとして99MBまで使用できます。99MBを超えるとswapファイルの拡張が自動的に発生します。
swap(仮想メモリ)のサイズを変更
一般的にswapファイルのサイズは、サーバに搭載されているメモリと同じとするのが通説です。
ちゃんとチューニングをしているサーバであれば、swapファイルに大きなサイズを指定する必要はありません。
搭載メモリが4GBのRaspberry Pi4の場合、1GBもあれば保守作業が十分行えると考えられswapファイルを1024MB(1GB)に拡張します。
手順は
- swap(仮想メモリ)サービスの停止
- 設定ファイルの変更
- swap(仮想メモリ)サービスの起動
- 状態の確認
で進めます。
swap(仮想サービス)サービスの停止
下記コマンドでswap(仮想メモリ)サービスを停止します。
$ sudo swapoff --all
$ sudo systemctl stop dphys-swapfile設定ファイルの変更
下記コマンドで、設定ファイルの内容を変更します。
$ 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=1024
# 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=2048
~赤字の通り1024MB(1GB)に変更します。
2048MB(2G)以上にしたいときは、併せて CONF_MAXSWAP の行頭の#を消して、値を同じ値に変更してください。指定がないときは、32bit向けに2Gの制限が入るようになっています。
swap(仮想メモリ)サービスの起動
下記コマンドでサービスを起動します。
$ 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; vendor preset: enabled)
     Active: active (exited) since Sun 2022-11-27 16:12:44 JST; 4min 31s ago
       Docs: man:dphys-swapfile(8)
    Process: 1122 ExecStart=/sbin/dphys-swapfile setup (code=exited, status=0/SUCCESS)
    Process: 1165 ExecStart=/sbin/dphys-swapfile swapon (code=exited, status=0/SUCCESS)
   Main PID: 1165 (code=exited, status=0/SUCCESS)
        CPU: 137ms
Nov 27 16:12:44 raspberrypi systemd[1]: Starting dphys-swapfile - set up, mount/unmount, and delete a swap file...
Nov 27 16:12:44 raspberrypi dphys-swapfile[1122]: want /var/swap=1024MByte, checking existing: deleting wrong size file (104857600), generating swapfile ... o>
Nov 27 16:12:44 raspberrypi systemd[1]: Finished dphys-swapfile - set up, mount/unmount, and delete a swap file.前章で設定した通り1024MByte(1GB)でswapファイルが作成されactiveになっている事が確認できます。
更に下記コマンド、設定どおり仮想メモリ1GBで起動している事を確認します。
$ sudo swapon -s
Filename                                Type            Size    Used    Priority
/var/swap                               file            1048572 0       -2
$ sudo free -h
               total        used        free      shared  buff/cache   available
Mem:           3.7Gi       173Mi       3.2Gi       6.0Mi       297Mi       3.5Gi
Swap:          1.0Gi         0B       1.0Gi
 
  


