1

I am looking to enable hibernation on Ubuntu 18.04.

I'd like to know if my swapfile exists and is being recognized as a swapfile. Not sure if I have swap, or if one part of the system does not recognize swap, or if I do not have swap.

$ sudo filefrag -v /dev/nvme0n1p4
Filesystem type is: 1021994
File size of /dev/nvme0n1p4 is 0 (0 blocks of 4096 bytes)
/dev/nvme0n1p4: 0 extents found

$ swapon -s Filename Type Size Used Priority /dev/nvme0n1p4 partition 16191484 0 -2

Ubuntu Disk Driver shows a 16G partition for swap under the same.

TIA.

wip
  • 11
  • 1
  • Swap is usually only used when you are close to running out of physical memory. Can you use a process monitor like htop and tell us what percentage of your physical RAM is in use? – Nmath Mar 09 '21 at 21:04
  • 1
    @Nmath: Nothing could be further from the truth; the Linux kernel likes to use swap space, even when there is plenty of free RAM. It depends on the kernel's swappiness setting. – HuHa Mar 09 '21 at 21:11
  • That doesn't make a lot of sense because physical RAM is almost always dramatically faster than swap. – Nmath Mar 09 '21 at 21:12
  • 1
    @Nmath: Google swappiness. – HuHa Mar 09 '21 at 21:13
  • The point of swap is to allow for the use of virtual memory when there is little-to-no physical memory. Yes, you can configure how swap behaves, but the default behavior mirrors the intended function of swap. – Nmath Mar 09 '21 at 21:16
  • 1
    @Nmath: https://askubuntu.com/questions/157793/why-is-swap-being-used-even-though-i-have-plenty-of-free-ram and https://unix.stackexchange.com/questions/2658/why-use-swap-when-there-is-more-than-enough-free-space-in-ram#:~:text=It%20is%20normal%20for%20Linux,and%20some%20other%20inactive%20daemon). – HuHa Mar 09 '21 at 21:17
  • None of that contradicts what I've said... Questions that ask why swap is not behaving as intended/desired does not indicate that swap is prioritized before physical memory. – Nmath Mar 09 '21 at 21:21
  • 1
    @Nmath youare wrong because swap is used to save memory not when memory is full only – not my real name Mar 09 '21 at 21:22
  • Considering that swap is much slower than physical memory, it would be a pretty ridiculous way to design a system. Unused RAM is wasted RAM. Swap is backup "second tier" virtual RAM. It is not prioritized above use of physical RAM unless specified. There are obviously situations where swap could be used when RAM is not near its capacity- some of those situations are described in the answers linked by HuHa, but this is the exception, not the rule. This is a silly thing to argue about and the other comments here run counter to common sense. – Nmath Mar 09 '21 at 21:29
  • 1
    @Nmath It doesn't matter if you don't agree this is the truth. I can disagree that the sky is blue and perhaps if I created the world I would make it yellow but that doesn't change the truth – not my real name Mar 09 '21 at 21:31
  • From the official documentation on community help: What is swap? Swap space is used when your operating system decides that it needs physical memory for active processes and the amount of available (unused) physical memory is insufficient. Note that the access time for swap is slower, depending on the speed of the hard drive. Do not consider it to be a complete replacement for the physical memory. – Nmath Mar 09 '21 at 21:36
  • @Nmath That is a lie, read up about the page cache – not my real name Mar 09 '21 at 21:37
  • Ok now the official documentation is a lie! Have a nice day. – Nmath Mar 09 '21 at 21:38
  • Hi, sorry to bump in but I am still trying to find my solution. Based on htop, system has 1.2/7.4G of physical RAM in use, 0.0/15.4G of Swapfile in use. But I'm not sure why File size of /dev/nvme0n1p4 is 0 (0 blocks of 4096 bytes) instead of the full 15 gigs. – wip Mar 10 '21 at 03:31
  • Why don't you "stress test" your swap to see if it's being utilized. See this question on Unix & Linux. As far as the discrepancy in the reported size on your formatted volume (15.4G vs 16G), you should ask about/research that separately. There are differences in how file size is calculated. Also, when you freshly format a partition, there is immediately overhead that reduces the available space on the drive: for example, the file system – Nmath Mar 10 '21 at 05:41
  • Great idea, will try it out! RE: discrepancy -- sorry should've been more clear. It's 15.4G vs 0G -- swapon says it's 15.4, filefrag says it's 0. That's why I was thinking, 'if filefrag doesn't "see" my swapfile, what other system processes are not seeing it?' Anyhow, thanks will try the stress test. – wip Mar 10 '21 at 06:06
  • 2
    also check out the difference between a swap file and swap partition. It seems that your swap is a partition, and in that case your system probably doesn't use a swap file – Nmath Mar 10 '21 at 06:54

1 Answers1

0

You can list the active swap devices with

swapon --show

or with

cat /proc/swaps

If you created your swap file as a sparse file (https://en.wikipedia.org/wiki/Sparse_file), it is possible that it actually has 0 blocks allocated if it never was in active use so far. Check its allocated size with

du -h myswapfile

and compare with

ls -lh myswapfile

(I guess that's what you wanted to achieve with that filefrag -v command which does something similar by coincidence)

HuHa
  • 3,385
  • Going back to my original post, swapon -s shows that there is indeed a 16G swap. But a call to filefrag sudo filefrag -v /dev/swapfile shows a 0G size of that particular swapfile. I have seen others run filefrag and get same results as swapon. Any idea why my filefrag and swapon are different? – wip Mar 09 '21 at 23:40
  • @wip maybe the problem is that filefrag tries to report file fragmentation on files on a filesystem, but a swap partition or file is not a traditional filesystem with files. – Soren A Mar 10 '21 at 09:30
  • @wip: See my edited answer. They like to have all information in one place on AskUbuntu.com, so it's customary to add additional information to the question and additional parts of an answer to the answer; the purpose being that the next user with the same problem doesn't have to piece together information from a whole discussion thread. – HuHa Mar 10 '21 at 09:33