Zram vs swapfile

I followed first part of the above video to to assign SATA as boot drive again but this time I didn’t follow the second part to rebuild the kernel.

After creating a swapfile, I enter “swapon -s” to check it and the result is as follows:
Filename Type Size Used Priority
/dev/zram0 partition 669876 0 5
/dev/zram1 partition 669876 0 5
/dev/zram2 partition 669876 0 5
/dev/zram3 partition 669876 0 5
/dev/zram4 partition 669876 0 5
/dev/zram5 partition 669876 0 5

Which one is really to be used, swapfife or zram?
The swapfile size is 8.6G.

We don’t have experience on about using the tool. Would need other users to check and suggest next.

swapon depends on the partition being listed in “/etc/fstab”. Did you add the swap information there?

The entry in fstab would be something like this (I am assuming it is “/dev/sda2”, but I have no idea what it really is on your disk; adjust for your case):
/dev/sda2 none swap sw 0 0
(there are other possibilities, that is just one typical, e.g., a UUID could be used as well)

The swapfile is in /mnt, about 8.6G.

I copied the eMMC with the command “cp -ax / ‘/media/username/sda1’ && sync”.
I was told that the swap partitions could not be copied; therefore, I created the swapfile with the following commands:

sudo su
cd /mnt
fallocate -l 8G swapfile
sudo chmod 600 swapfile
sudo mkswap swapfile
sudo swapon swapfile
exit
free -m

I want to know which one is in use, swapfile, zram or swap partitions in eMMC?

What do you see from “swapon --show” (“-s” is summary, I’m thinking “--show” is different)?

Incidentally, if your commands were used, and assuming it works, then this will become inactive at the next reboot. Additionally, “/mnt” content is for a mount point, and is a bad place for a swap file. Anything which mounts there would cause the swap file to disappear until umount. One would not normally add files to “/”, but for swap, I’ll suggest it is best there (this makes it available at the earliest possible point, and only an initrd would have the ability to get in the way of this).

Should I delete the swapfile and create a new one?

Is the dev/sda2 a real partition or a folder?

I assume the zram is in use but not really sure.

/dev/sda2 is just a contrived example of a partition. Since you are using a swap file and not a swap partition, I would suggest creating it in “/”, e.g., you could have a file which is “/swapfile”. The difference is that it would be in “/” instead of “/mnt”.

Even so, after all of your commands, check if you see it from “swapon --show” before you reboot. If it shows up correctly, then you can add it to “/etc/fstab”, e.g., with a line similar to this:
/swapfile none swap sw 0 0

It can be bad if you add content to fstab which does not work; there are some conditions under which boot will not continue until the fstab target is mounted. It is also possible that an initrd might still require more work.

Thank you.

[quote=“linuxdev, post:5, topic:262201”]
What do you see from “swapon --show” (“-s” is summary, I’m thinking “--show” is different)?
The outcomes are different and the same.
swapon -s
Filename Type Size Used Priority
/dev/zram0 partition 669876 0 5
/dev/zram1 partition 669876 0 5
/dev/zram2 partition 669876 0 5
/dev/zram3 partition 669876 0 5
/dev/zram4 partition 669876 0 5
/dev/zram5 partition 669876 0 5

swapon --show
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 654.2M 0B 5
/dev/zram1 partition 654.2M 0B 5
/dev/zram2 partition 654.2M 0B 5
/dev/zram3 partition 654.2M 0B 5
/dev/zram4 partition 654.2M 0B 5
/dev/zram5 partition 654.2M 0B 5

I think the zram partition or folder is not existed before the system startup. I searched the eMMC for the zram files but could not find them.


The “SD” icon on the sidebar is the shortcut of eMMC.

zram is actual RAM used to pretend it is a disk. This is fast and does not wear out solid state memory. So this is unrelated to the current problem whereby it is ignoring your manually created swap.

Did you move your swap file to “/”? Other than that the actual creation of a swap file procedure you used previously should work well. The next step is activation. What is the exact name of the swap file, and location?

Assuming your swap file is “/swapfile” (in “/”, not “/mnt”), what is the output of:
file /swapfile

Also, what do you see from:
zcat /proc/config.gz | grep -i swap

file /swapfile
/swapfile: data

zcat /proc/config.gz | grep -i swap
#CONFIG_SWAP=y
#CONFIG_MEMCG_SWAP=y
#CONFIG_MEMCG_SWAP_ENABLED=y
#CONFIG_FRONTSWAP is not set
#CONFIG_MTD_SWAP is not set
#CONFIG_NFS_SWAP is not set

swapon -s
Filename Type Size Used Priority
/dev/zram0 partition 669876 1224 5
/dev/zram1 partition 669876 1224 5
/dev/zram2 partition 669876 1228 5
/dev/zram3 partition 669876 1224 5
/dev/zram4 partition 669876 1224 5
/dev/zram5 partition 669876 1224 5
/home/p/swapfile file 8388604 0 -1

swapon --show
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 654.2M 1.2M 5
/dev/zram1 partition 654.2M 1.2M 5
/dev/zram2 partition 654.2M 1.2M 5
/dev/zram3 partition 654.2M 1.2M 5
/dev/zram4 partition 654.2M 1.2M 5
/dev/zram5 partition 654.2M 1.2M 5
/home/p/swapfile file 8G 0B -1

sudo swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
swapon: /swapfile: read swap header failed

I did it once more and made a little bit change, the result is as follows:

sudo swapon /swapfile

sudo file /swapfile
/swapfile: Linux/i386 swap file (new style), version 1 (4K pages), size 2097151 pages, no label, UUID=f0a277ae-29f4-48f3-92be-c249a8a259a1

I think the system uses temp files as virtual memory if swapfile is not existed.

Is this what you actually see:

I ask because of the leading “#”. This comments out the line. If CONFIG_SWAP and the others are really “=y”, then it won’t have a leading “#”. I don’t think it should ever happen where there is a leading “#” and also “=y”. This is from a TX2:

# zcat /proc/config.gz | grep -i swap
CONFIG_SWAP=y
CONFIG_MEMCG_SWAP=y
CONFIG_MEMCG_SWAP_ENABLED=y
# CONFIG_FRONTSWAP is not set
# CONFIG_MTD_SWAP is not set
# CONFIG_NFS_SWAP is not set

I’m hoping the leading “#” is something you added. If it really is a leading #, then this feature is really “=n” despite saying “=y”.

However, the new “swapon -s” output has this:
/home/p/swapfile file 8388604 0 -1

Did you add a swap file in “/home/p/swapfile”?

No swap is used unless it is needed, and no swap source is used if it does not pre-exist. It is true that different swap can have different priorities, but all swap would still be listed even if it is not used (assuming it is activated).

Temp files are not the same as virtual memory. Those are determined by the design of the running software.

What is the content of your “/etc/fstab”?

Incidentally, if you highlight any log or file content you paste into the forum, and then click on the “code” icon (looks like “</>”), then it will preserve whitespace and not be interpreted when odd characters are in it.

[quote=“linuxdev, post:11, topic:262201, full:true”]
Is this what you actually see:

Definitely. I didn’t add the “#” but deleted the “space” between # and CONFIG to make the size of the words normal.
You see the words below are big because of the space between # and the following word.

CONFIG_FRONTSWAP is not set

Yes.

cd /etc/fstab
bash: cd: /etc/fstab: Not a directory

If the swapfile does not enable what happen if the system run out of memory?

This is definitely wrong. Has the kernel ever been changed? You should never see a commented out symbol which is also either “=y” or “=m”. It isn’t the space I am wondering about, it is the leading hash mark, “#”. This says the configuration is inert, and equivalent to not set.

/etc/fstab” is the full path to a file, and is not a directory. You would:

cd /etc
cat fstab

(or just upload “/etc/fstab” as a file to the forum)

It is fascinating that your swapon worked and is doing what you want for the latest case of “/home/p/swapfile”, and yet the config shows features commented out.

Also, the “/swapfile” is preferred. The problem noted on security is that the swap file needs correct permissions. It was probably set readable by a non-root user.

If the system runs out of memory, then it will possibly do one of these:

  • Use the “Out of Memory” killer to kill applications (known as “OOM”).
  • Lock up and become unusable.
  • Panic.

However, swap is not everything you think it is. It won’t solve some situations. Here is some background…

When a computer boots, before end users get to log in, devices are accessed via a physical absolute address. Those addresses are hard wired or in firmware. Later plug-n-play devices can be used which are more flexible. The PCIe bus itself is generally hard wired or based on firmware, but the cards in the slots of the PCIe bus are plug-n-play and don’t generally need the kernel to find them in some blind manner.

The GPU on a Jetson is integrated directly to the memory controller (making it an integrated “iGPU”). Its access is not PCIe, and it is found via either hard wiring an address or via firmware (such as a device tree).

Such devices are communicated with via a “device driver”. Those drivers use an actual exact physical address.

End user programs use a virtual address. When memory is assigned or allocated to a user program, the lowest address is 0x0 for all of them. The memory controller is using a bit of magic to make the programs think the memory is between 0x0 and some other address range. The memory might even be split up (fragmented) into multiple smaller chunks, and the memory controller is giving that end user program the illusion that the memory is one contiguous, unbroken chunk.

Drivers with physical addresses cannot use broken chunks in many cases, even if the memory controller uses its magic to rearrange things. End user programs can. Swap files and zram are virtual. They are part of a fragmented pool of memory which the memory controller can use to give separate fragments the illusion of being one big contiguous pool without breaks.

So if your end user program allocates too much memory, but you have swap, then you’re ok because swap will be added to the pool. You could eventually run out of swap though. Then your program will be killed via the OOM (it is actually a bit more complicated because program priorities might alter what gets killed).

If your end program depends on physical addresses, then swap will never take part. Swap won’t help there. This includes CUDA based programs because the GPU is using physical addressing. No matter how much swap you add, the GPU will never benefit directly from swap. It is true though that some other user space program could be killed, and this would allow some more memory for the GPU, but that is indirect, and it is not guaranteed to help (the new memory might be fragmented in such a way that the GPU still could not use it, or it might use only a subset of what the user space program’s kill released).

So if you have a program in user space, this is when swap helps. If you have CUDA or AI which is running out of memory in the part which uses physical addressing, then your swap will not help.

If you are doing this because of running out of memory, then you might state under what circumstances it is running out. There might be nothing you can do about it (although perhaps redesigning a CUDA code block could use less memory, e.g., using fewer CUDA cores could lead to not using as much physical addressing).

Thanks for your detailed explanation.

file system mount point type options dump pass
/dev/root / ext4 defaults 0 1

Could you try this to switch to the “/swapfile” instead of the one in your home directory?

  • If swap is currently running, “sudo swapoff /whatever/file/it/is”. Basically, disable any swap other than “/swapfile”. Check that “sudo swapon --show” and “free -h” do not see any ordinary swap files (zram can be ignored, we’re only talking about swap files, and zram is not actually file).
  • I assume “/swapfile” still exists (if not, then create it like you did before in “/”). Set permissions correctly:
    sudo chmod 600 /swapfile
  • See if this works:
    sudo swapon /swapfile
    (Verify this is working with “sudo swapon --show”)

If all is good to that point, then add this to the end of the “/etc/fstab” file:

/swapfile   none   swap   sw,nofail   0    0

This line for fstab is standard with one exception: I added the “,nofail” (it is a comma-delimited list of options). The nofail says boot can continue without an error if the swap fails.

Now reboot, and see if swap from /swapfile is automatically enabled. The previous “permission” error should be gone since the “/swapfile” is owned by root and permission 0600 (root read-write, no exec).

Thank you.

sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -1
/dev/zram0 partition 654.2M 0B 5
/dev/zram1 partition 654.2M 0B 5
/dev/zram2 partition 654.2M 0B 5
/dev/zram3 partition 654.2M 0B 5
/dev/zram4 partition 654.2M 0B 5
/dev/zram5 partition 654.2M 0B 5

zcat /proc/config.gz | grep -i swap
CONFIG_SWAP=y
CONFIG_MEMCG_SWAP=y
CONFIG_MEMCG_SWAP_ENABLED=y

# CONFIG_FRONTSWAP is not set
# CONFIG_MTD_SWAP is not set
# CONFIG_NFS_SWAP is not set

it seems that the outcomes have changed.

How do I know if the swapfile is working.

I like the app “htop” (“sudo apt-get install htop”), which shows a “Swp” line near the top. However, the fact that “swapon --show” names “/swapfile file 8G 0B -1” implies it is available. You might also check the output of “cat /proc/swaps”.

Just remember that virtual memory is not used for CUDA and GPU operations (this and many device drivers require actual physical RAM).

The swapfile becomes 11G.

There you have it: Success. 😀

I am going to remind you though that this is a lot of swap, performance would suffer greatly when using that much compared to the 8 GB of RAM. Also, in the case of drivers, including the use of CUDA, swap will not be of any direct use (things like training models will run out of memory just as fast as they did without the swap).

Thank you.
I really appreciate it.