Jetson Nano システムがフリーズする! Swap 領域を作ってみた。

スポンサーリンク

Jetson Nanoを使っていてソフトがフリーズすることありませんか?今回はJetson Nanoに「 swap 」を作る手順を解説していきます。swapとは簡単に説明すると、メモリとストレージの内容を入れ替える動作のことです。詳しくは参考にしたサイトをご覧ください。

スワップとは - IT用語辞典
スワップとは、交換(する)という意味の英単語。ITの分野では、OSがメインメモリとストレージの内容を入れ替える動作や、機器に装着された装置の交換、コンピュータプログラムで二つの変数や領域のデータを交換する処理などを指す。OSのメモリ管理機能の一つで、メインメモリ(RAM)とストレージ(外部記憶装置)の同容量の領域間でデ...
スポンサーリンク

swap 作成

swap を作る準備をしていきます。作業はJetson Nanoで行います。自分でコマンドを入力して swapを作るのは大変なので有名なスクリプトを利用して作成します。GitHubからダウンロードします。

GitHub - JetsonHacksNano/installSwapfile: Install a swap file on the NVIDIA Jetson Nano Developer Kit. This should help with memory pressure issues.
Install a swap file on the NVIDIA Jetson Nano Developer Kit. This should help with memory pressure issues. - GitHub - JetsonHacksNano/installSwapfile: Install a...

スクリプトをダウンロードします。

$ git clone https://github.com/JetsonHacksNano/installSwapfile.git
$ cd installSwapfile

ダウンロードしたファイルの中に[installSwapfile.sh]があります。ここを編集して swap のサイズを変更することができます。デフォルトでは6GBの swap が作成されます。

#!/bin/bash
# Copyright 2017-2019 JetsonHacks
# MIT License
# Create a swap file and set up permissions
# If a parameter is passed, it should be the place to create the swapfile
set -e
SWAPDIRECTORY="/mnt"
# Ubuntu recommends 6GB for 4GB of memory when using suspend
# You can use 1 or 2 if need be
SWAPSIZE=6
AUTOMOUNT="Y"
function usage
{
    echo "usage: installSwapFile [[[-d directory ] [-s size] -a] | [-h]]"
    echo "  -d | --dir <directoryname>   Directory to place swapfile ( default: /mnt)"
    echo "  -s | --size <gigabytes> (default: 6)"
    echo "  -a | --auto  Enable swap on boot in /etc/fstab (default: Y)"
    echo "  -h | --help  This message"
}

while [ "$1" != "" ]; do
    case $1 in
        -d | --dir )            shift
                                SWAPDIRECTORY=$1
                                ;;
        -s | --size )           shift 
				SWAPSIZE=$1
                                ;;
        -a | --auto )           shift
				AUTOMOUNT=$1
				;;
        -h | --help )           usage
                                exit
                                ;;
        * )                     usage
                                exit 1
    esac
    shift
done

echo "Creating Swapfile at: " $SWAPDIRECTORY
echo "Swapfile Size: " $SWAPSIZE"G"
echo "Automount: " $AUTOMOUNT

#Create a swapfile for Ubuntu at the current directory location
sudo fallocate -l $SWAPSIZE"G" $SWAPDIRECTORY"/swapfile"
cd $SWAPDIRECTORY
#List out the file
ls -lh swapfile
# Change permissions so that only root can use it
sudo chmod 600 swapfile
#List out the file
ls -lh swapfile
#Set up the Linux swap area
sudo mkswap swapfile
#Now start using the swapfile
sudo swapon swapfile
#Show that it's now being used
swapon -s

if [ "$AUTOMOUNT" = "Y" ]; then
	echo "Modifying /etc/fstab to enable on boot"
        SWAPLOCATION=$SWAPDIRECTORY"/swapfile"
        echo $SWAPLOCATION
	sudo sh -c 'echo "'$SWAPLOCATION' swap swap defaults 0 0" >> /etc/fstab'
fi

echo "Swap file has been created"
echo "Reboot to make sure changes are in effect"

マーカを引いたところの「6」という数字を変更するとその数字分の swap が作成されます。変更したら保存し以下のコマンドを実行することで swap が作成されます。

$ ./installSwapfile.sh

処理が終了すると Swap が作成されていると思います。
システムモニターで確認すると作られているはずです。

まとめ

今回は Swap の作成方法について解説しました。スクリプトのおかげで簡単に Swap を作成することができました。これで Jetson Nano を快適に使うことができると思います。

コメント

タイトルとURLをコピーしました