Jetson Nano – Use More Memory!

The NVIDIA Jetson Nano Developer Kit has 4 GB of main memory. This may not enough memory for running many of the large deep learning models, or compiling very large programs. Let’s fix that! We are going to install a swapfile. Looky here:

Background

There may be times when you are running your Jetson Nano when the screen suddenly freezes. You may have a lot of browser tabs open, or be running multiple YouTube videos, or other memory hungry applications.

If you are a developer, or are loading large trained models, sometimes in the Terminal console you will see your program aborted with the simple word ‘Killed’. More than likely, this is because you ran out of memory.

The Jetson Nano has 4 GB of RAM. Sometimes this is not enough for big jobs. Fear not! There is a feature in the Linux kernel, called a swapfile, which implements paged memory. You can read all about swapfiles here: Ubuntu Swap FAQ.

Installation

On the JetsonHacksNano account on Github, there is a repository named installSwapFile. Clone the repository, and then switch over to the repository directory:

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

Here’s the usage instruction:

usage: installSwapFile [[[-d directory ] [-s size] -a] | [-h]]

All of the arguments are optional. The default is for a 6GB swapfile to be created in the directory /mnt. The -a flag indicates whether the swapfile should be automatically be loaded on boot. If the swapfile is to be loaded at boot time, make sure that the location is mounted when the machine boots.

Here’s an article on how to setup automount for external devices.

You can run the default:

$ ./installSwapfile

and a 6 GB swapfile will be installed at /mnt/swapfile

Note: You will need to have enough extra room on your device to install the swapfile.

For the 4 GB Jetson Nano, the 6 GB swapfile is what Ubuntu recommends assuming that you are using hibernation. Otherwise 2 GB should do.

In the video, the swap file is auto mounted when the machine boots. This is great for development, but afterwards you may want to disable that feature. To do so:

$ sudo gedit /etc/fstab

and comment out the line that does the ‘swapon’. Make sure to save the file, reboot and check to make sure that swap is off. 

Discussion

It’s been noted in the comments that many people are against using a swapfile on the micro SD card. This is a for a variety reasons (mostly due to wear patterns and eventual failure of the SD card, and poorer performance), and is certainly a valid viewpoint.

There is certainly alternative, equally valid, viewpoints. Some of the previous experience that people mention comes from using a Raspberry Pi for long periods of time. One note, the Raspberry Pi in general have 1GB or less of memory; a Jetson Nano has 4GB. It is unlikely it will be swapping memory as much. Also, you can set the “swappiness” lower so that Linux is less eager to swap memory.

Of course, you are not limited to keeping your swapfile on a SD card, you could put it on a USB SSD. The SSD has better wear leveling algorithms. As an alternative, you could even use a USB Hard Disk Drive (HHD) which isn’t as susceptible to this issue.

You are also not forced to always use a swapfile. You may only want to use it for certain tasks, such as compiling large programs, or loading large deep learning models.

Certainly a developer/power user wants to be able to use the resources available to them. Professional users view removable media as consumables, and do not hesitate using/abusing them for their full life time. They also make backups in case of failure.

Conclusion

This is a pretty simple way to make your Jetson Nano much more responsive, and provide more memory for those large builds and deep learning models.

18 Comments

  1. If one uses a swapfile does it increase the power draw ?? I’m assuming it does, i ask because im building a 18650 battery pack for the nano, and i want to calculate the power consumption for the battery pack correctly.

    • I would assume that using the micro SD card and processor use more energy. Specifically how much energy is a different question. I do not know if that is included in the 10W power envelope that NVIDIA states for the module. You can ask about energy usage for the microSD card reader on the official NVIDIA Jetson forum where a large number of developers and NVIDIA engineers share their experience. The forum is here: https://devtalk.nvidia.com/default/board/371/jetson-nano/
      Thanks for reading!

  2. Hi, thank you for this tutorial. I just followed it to generate a swapfile in an HHD. But when I tried to use the GPU to train a model in nano, it still shows that the total memory is 4G, and then finally out of memory. It seems like the swapfile I allocated doesn’t work. Is there anything I can do to fix that?

    • You can check to see if swap is enabled by opening the ‘System Monitor’ application from the Ubuntu Launcher. There should be a green ring in the ‘swap file’ area.

    • There isn’t any information here. What devices are you referring to? What size SD card are you using? Did you set up your swap file immediately after flashing the SD card?

  3. I needed a swap to compile opencv 4.1 on the Nano with jetpack 4.2.1. The script worked fine to enable swap. But I can’t seem to disable the swap file. Removing the entry from /etc/fstab didn’t work.

    Thanks for any advice.

  4. I used the swapfile to run inference in a TensorFlow-GPU object detection model. But it seems that the Nano doesn’t see and doesn’t use the swap. I know that it uses the GPU memory to this purpose.

    Is there a way to increase GPU memory with the swap?

    If I change to TensorFlow (CPU) will the swap work?

  5. Hello, my Jetson nano keep crashing while installing opencv 4.1, I verified that the swap is enabled, I don’t know what to do ? I keep receiving Memory warning

Leave a Reply

Your email address will not be published.


*