JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

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.

Facebook
Twitter
LinkedIn
Reddit
Email
Print

31 Responses

  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.

    1. 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?

    1. 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.

    1. 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. Do you know clone Jetson Nano SD in windows. I customized año Os bit i cant créate a clone.

  4. 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.

  5. 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?

  6. 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

  7. e have installed Jetpack 4.2.3 on jetson nano. This has created a 2 GB built in zram memory. We have additionally added 4 GB swap file using this post.
    The system is taking priority to zram. We want to set the newly created swap to take priority over zram. How to change Swap partition priority?

    1. A swapfile is different than swap memory. zram compresses memory making memory seem larger, a swapfile swaps memory pages to disk. If you do not want to use zram, disable it. The file /etc/systemd/nvzramconfig.sh configures zram at system startup on the Jetson. If your remove nvzramconfig.sh from the /etc/systemd folder, zram will not be invoked when the system boots. Thanks for watching!

  8. I did it well and got a 6GB of swapfile. How can I add 4GB?
    When I just changed installSwapfile.sh SWAPSIZE=10,
    I got below error messages.
    fallocate failed: Text file busy
    How can I add more memory well?

    1. You are welcome. It depends on what type of program you are trying to run on startup, and how you want to start it. You can use the startup applications GUI as one approach, or use systemd if you need finer control. Thanks for reading!

  9. I followed the tutorial but my swapfile is created for 11.9 gb . I did not alter any command but I checked under system monitor . It shows 11.9 gb swapfile with green dot.

      1. Thanks for the reply. I followed the same commands which you outlined in the video. May be I ran the commands twice by mistake. Is there a way to remove the swap file rather than Unmounting it? So that a fresh file can be created from scratch. Please excuse my command line ignorance. Thanks

  10. I am sorry for my poor English. I used swap files to use large deep running models. However, The analysis was very slow. Is this related to the reading speed of the sd card? So, is it helpful to change the sd card model for faster reading speed?

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