JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

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.sh [ [-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.sh -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
Facebook
Twitter
LinkedIn
Reddit
Email
Print

12 Responses

      1. I think marty means the article is missing the .sh at the end of the bash command when using it as it’s a shell script.

  1. Hi,kangalow.
    Is there any solution to expand the eMMC disk volume?
    16GB sometimes is too small for some deep learning networks.

    1. I am not sure what you are asking. The Nano developer kit does not have eMMC. Do you mean the Jetson Nano module? Typically if you want more storage, you would add external storage, such as a USB SSD. This has an advantage in that it is much faster than eMMC.

  2. I’m a bit confused, I tough zram was not able to use real disk as backing store (from wikipedia :’…zram cannot use a hard disk as a backing store’), so does’it mean that allowing 4GB of swap, means simply allowing to compress the whole ram (like in the old day of DOS memmaker) ?

    Should not be preferable to keep the default 2GB of zram, but to also allocate 4GB in a real swap file (with the old mkswap) ?

    1. You are correct. ZRAM is basically a compressed version of RAM. You would use a real swap file if you need additional virtual memory. Thanks for reading!

  3. Hi, I successfully modified the .sh file. But when I boot up, swap file size remain the same. Then I double check the .nvzramconfig.sh. And it do shows as a successfully modified file. I’m using newest jetpack4.3 on sd card.

  4. thanks for your post
    but i have a question
    how i can disable the swapfile and get back to normal situation ??

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer

Some links here are affiliate links. If you purchase through these links I will receive a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

Books, Ideas & Other Curiosities