Add Swap Space to VM instance of GCP

"RuntimeError: DataLoader worker (pid 4161) is killed by signal: Killed.
This is the error I've encountered when I was running Pytorch program. It means that the memory of the system is not big enough to run the program. Thus, I found there is no swap space in my VM instance. To fix this issue, I add new swap space to my system.


Environments

  • Ubuntu 16.04
  • Google Cloud Platform

The default size of the swap of the system is zero when you create an new instance.

Step 1: Check the size of swap

$ sudo apt-get update
$ apt-get install htop
Type this command after the installation is done.
$ htop

Step 2: Generate swap file

I am going to generate a 10-GB swap file. It is calculated as 1024 x 1024 x 10 = 10485760.
$ dd if=/dev/zero of=/home/chunming/swapfile bs=1024 count=10485760
10485760
10485760+0 records in
10485760+0 records out
10737418240 bytes (11 GB, 10 GiB) copied, 33.144 s, 324 MB/s
where if represents input file, and of represents output file. bs specifies the block size for the both the input and output file.

Step 3: Change to root

$ sudo su


Step 4: Set up a Linux swap area

# mkswap /home/chunming/swapfile
Setting up swapspace version 1, size = 10 GiB (10737414144 bytes)
no label, UUID=6ee87969-4dd2-4df0-ab06-e97673b40804


Step 5: Modify system configuration

# vi /etc/fstab

Add this line to the file and then save.
/home/chunming/swapfile  swap   swap   defaults  0  0


Step 6: Enable devices and files for paging and swapping

# swapon -a
swapon: /home/chunming/swapfile: insecure permissions 0664, 0600 suggested.
swapon: /home/chunming/swapfile: insecure file owner 1003, 0 (root) suggested.


Step 7: Reboot instance

# reboot

Check the swap again

$ htop

Now the size of the swap is 10 GB.

References:

Share:

0 意見:

張貼留言