Jetson Nano – Even More Swap

Swap memory is now part of the default NVIDIA Jetson Nano distribution. Here’s how to change the amount of swap memory in use. Looky here:

Background

Recent releases of L4T/JetPack enable swap memory as part of the default distribution. You may remember an earlier article on JetsonHacks, Jetson Nano – Use More Memory! which describes how to implement a swap file on earlier versions of L4T which did not have this feature enabled.

The current JetPack distribution uses the zram module for managing the swap memory. You can read more in depth about zram on Wikipedia. Zram does some clever things in maximizing compression and performance when using ramdisk/swap disk. There’s a lot under the hood. More than we will cover here, but the reference material is out there if you want to read up on it.

In the default distribution, the swap memory size is 2GB. At times this may not be enough if you are doing very large projects. For example, building OpenCV from source. Let’s go over how you might expand/contract the swap memory.

Note that these techniques are most useful on external drives on the Jetson (USB SSD and hard drives).

Setting Up

The Jetson Nano by default has 2GB of swap memory. The swap memory allows for “extra memory” when there is memory pressure on main (physical) memory by swapping portions of memory to disk. Because the Jetson Nano has a relatively small amount of memory (4GB) this can be very useful, especially when, say, compiling large projects.

In the video, we use:

  • Jetson Nano
  • Samsung T5 USB Drive
  • A 5V, 4A power supply

You can support the channel by shopping for these items on the JetsonHacks Amazon store front! The shop has these items available. There’s no charge to you, and the channel gets a small commission. Thanks!

In the video, we are using the USB drive as the root file system.

The swap memory method in use is Zram. You can examine the swap memory information:

$ zramctl

You will notice that there are four entries (one for each CPU of the Jetson Nano) /dev/zram0 – /dev/zram3. Each device has an allocated amount of swap memory associated with it, by default 494.6M, for a total of around 2GB. This is half the size of the main memory.

The configuration for the Zram allocation is done on startup. The file that controls this is /etc/systemd/nvzramconfig.sh

The size of the Zram for each CPU is calculated by the line:

mem=$(((“${totalmem}” / 2 / “${NRDEVICES}”) * 1024))

where totalmem is the total amount of memory, and NRDEVICES is the number of CPUs.

Basically it divides the amount of physical memory by the number of CPUS with a divisor, in this case 2 to get the 2GB total. You can simply edit this equation using a text editor. You should probably make a backup of the file first, just in case. You will need sudo permissions to change the file.

sudo gedit /etc/systemd/nvzramconfig.sh

For example, you may remove the divisor to get a full 4GB.

On the JetsonHacksNano account on Github, there is a repository named resizeSwapMemory. The repository contains a convenience script to set the size of the swap memory.

$ git clone https://github.com/JetsonHacksNano/resizeSwapMemory

$ cd resizeSwapMemory

To use the script in the repository:

usage: ./setSwapMemorySize [ [-g #gigabytes ] | [ -m #megabytes ] | [ -h ]
  -g #gigabytes – #gigabytes total to use for swap area
  -m #megabytes – #megabytes total to use for swap area
  -h – help 

Example usage:

$ ./setSwapMemorySize -g 4

will set the entire swap memory size to 4GB.

This will modify the /etc/systemd/nvzramconfig.sh to set the requested memory for the swap memory as specified.

You will need to reboot for the change to take effect.

Notes

You will need to have the amount of memory that you specify for the swap memory available on disk. The recommended swap memory size is 2GB for a 4GB Jetson Nano. Larger swap memory sizes can sometimes cause decreased performance. You may want to switch swap memory size for specific tasks (such as compiling a very large program) and then revert to the default size of 2GB afterwards.

Tested on Jetson Nano

  • Jetson Nano
  • L4T 32.2.1/JetPack 4.2.2
  • L4T 32.2.3/JetPack 4.3

2 Comments

Leave a Reply

Your email address will not be published.


*