The problem that swap keeps increasing in jetson nano

Every time I run a program or compile some source code, the Mem space is not fully used and part of the swap space is used. After I closed the program, the swap space was not released. I want to know what the role of swap space is and why the swap space is not released.


Welcome to the NVIDIA Developer forums! Your topic will be best served in the Jetson category.

I will move this post over for visibility.


Swap space is “virtual” memory. This is used only in user space programs (not drivers). All of the reasons is a rather large topic, but before I mention that, I’ll make some notes about buffer and cache memory (which have a principle that is similar, but easier to understand due to being less complicated “under the covers”).

Any time you have RAM which isn’t being used it is wasted. You’ve heard of CPUs themselves having cache RAM, and more expensive CPUs have more cache, often at different levels. That is dedicated cache. Any time you read or write a file in the file system it takes time to access the disk. If the file is read once, and from then on stored in RAM until changed, then future reads and writes are much faster. The trick becomes one of making sure the actual disk gets written before the system shuts down. Simply putting this in RAM is what cache or buffer might do, and swap is something of a variant in reverse.

Since that cache or buffer memory is not mandatory, but is being attached to things which have a more authoritative source (but a source which is slower), it implies the memory is available on short notice. The cache or buffer can have whatever it applies to written/flushed out to its parent device. Then the RAM becomes available. The system does not know when “pressure” will become high enough that something of higher priority will need RAM, so it just stays there as cache or buffer until conditions cause release. There is no harm or loss because the RAM was not being used for anything. This is not dedicated RAM, it has a temporary use.

Swap is the inverse. There isn’t enough RAM, and so the system puts that memory onto a disk device (it isn’t always really a disk, although that memory is treated as a disk…more on this below). The swap space is unused, so there is no need to free it. The CPU needs more memory for something that is perhaps low priority, and running in the background, so there isn’t much of a performance hit for this process that isn’t yet scheduled to run (remember this is “multitasking”…things may seem continuous, but the CPU cores are switching processes constantly, and it is more like fast frames of a movie making the system appear continuous and smooth).

Imagine for example a web browser that has several tabs open, but only one is in the foreground. If you don’t have enough RAM to also run drivers (such as networking that feeds the browser), then you’d have to kill that user space program/process. Instead of killing it, why not put its RAM requirements on disk (at least for the thread or process which is in the background)? Until you bring that back to the foreground there isn’t any need to release it. If you’ve exited that browser, then you can perform a lazy release since release may in fact still take CPU cycles.

Usually though swap is disk storage with loopback covering it to look like physical RAM. Loopback can make block devices and memory devices change to look like something else despite being backed by either physical RAM or disk. Loopback is the great pretender. Sometimes one takes a chunk of RAM and makes it look like a disk. That seems like a bad idea because why would one swap from RAM to RAM? Not all programs or software know better and might try to swap even when there is plenty of RAM. Be very careful when you look at swap to see which device is backing that swap…it might be a ramdisk via loopback. Check this comand:
lsblk -f

See anything that is a loop device?

Another purpose for ramdisk is to save wear on solid state memory. If a program is constantly writing to disk, even though RAM is available, the solid state memory will wear out sooner. That’s one reason for adding swap in the form of ramdisk instead of actual disk backing.

Swap is normally backed by a fixed chunk of memory. That memory might be on a disk as a file, or it might be on a disk as a partition. It might even be loopback covering RAM that looks like a partition, but it is a fixed size dedicated to swap. Releasing that swap does not make the backing device become available. If swap has filled, then it does release for something else to swap. Unlike buffer or cache, which is the inverse of swap, you don’t get the backing store returned. The need of other programs to use swap, when swap is running out, is where the pressure comes to release swap. The need of programs or drivers to use physical RAM is what drives the pressure to release RAM in any temporary use, e.g., programs needing RAM will cut back on buffer/cache of disk operations, and you’ll see that.

An interesting program is xosview (you can “sudo apt-get install xosview”). It is a GUI memory and load indicator. You can drag the edge or corner to enlarge this. You’ll see memory use separated for types of use. That marked “USED” is actually used as is. This is something the system can’t stop use of without killing something. What you see as “BUFF” or “CACHE” isn’t really used, it is available, but something else will have lower performance when it gives up that RAM. In xosview swap does not show “different uses” because it is pretending to be only one type of memory: RAM. The total available swap though will not change regardless of whether something is using it or not (that’s because what is backing it matters; killing an application that used swap won’t delete partitions).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.