What's using space in /swap ?
Posted by I0I0I0I@reddit | linuxadmin | View on Reddit | 34 comments
I've disabled swap on this system for the purpose of trying to answer this question. What's consuming 16G in /swap? Lubuntu 22.04.
zzyzx@zzyzx: [ ~ ]$ ls -al /swap/
total 0
drwxr-xr-x 1 root root 0 May 29 13:30 .
drwxr-xr-x 1 root root 170 Sep 11 2024 ..
zzyzx@zzyzx: [ ~ ]$ df -h /swap/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb2 462G 16G 443G 4% /swap
zzyzx@zzyzx: [ ~ ]$ swapon --verbose --show
zzyzx@zzyzx: [ ~ ]$
zzyzx@zzyzx: [ ~ ]$ free
total used free shared buff/cache available
Mem: 8007708 3633660 1385412 276000 2988636 3794596
Swap: 0 0 0
craftsmany@reddit
The swap file itself is 16GB in size. If you don't use it delete it.
I0I0I0I@reddit (OP)
I already did. My goal is to eliminate swap files and use only partitions.
grumpysysadmin@reddit
You probably don’t want to put a filesystem on your swap partition and have it mounted. I don’t think the kernel will let you activate it as swap if it’s a mounted filesystem, since the device will be in use.
Make sure you remove any mention of a /swap mount point in your fstab. There probably was an entry for swap in there, but it wasn’t mounted with a filesystem on /swap, so I don’t know if you deleted the original line or left it there.
I0I0I0I@reddit (OP)
There's no filesystem on a swap partition. It's a BTRFS subvolume mounted on /swap that originally contained nothing but a 512M swapfile, which I have deleted.
grumpysysadmin@reddit
Then I hope you’ve removed the sub volume mount entry from the fstab.
craftsmany@reddit
Did you try to format that partition?
I0I0I0I@reddit (OP)
How would that tell me what's using 16Gb on the partition?
symcbean@reddit
There is no swap file here.
blin787@reddit
Also theres is no swap partition (mounted). Just /swap/ folder :) madness
I0I0I0I@reddit (OP)
BTRFS subvolume. Such confusion.
michaelpaoli@reddit
So ... you were using swap file(s), rather than a swap partition(s) or other block device(s) for swap? Did you then remove the files? Does anything still have them open?
I'm also not so familiar with btrfs, its overhead / reserved space, and how specifically df may report on that, so \~16G on 443G filesystem isn't all that much and may be quite typical overhead / reserved space for such on btrfs. So, are you able to umount your /swap filesystem? Because if you do, then nothing is using it (at least as a filesystem, anyway).
cknipe@reddit
I feel like a lot of people are trying to say things here about swap partitions and swap files. This is neither of those things. This is just some random partition (and a pretty big one at that) which someone has decided to mount on a directory called /swap. It's no different than any other mounted directory. Why is it called /swap? Who can know. But as for your question of determining what's using space...
A quick and dirty way to do it would be "cd /swap" followed by "du -ks * |sort -n". That'll show you your files/directories on that disk ordered by size. See if there's one particularly large one at the top. if it's a file, congratulations you found it. If it's a directory you cd into it and run the du again.
If you already know what file is taking up the space but you want to see who has it open you can do "lsof -n |grep"
If none of this is what you're actually trying to figure out, drop some more details and we'll try to help.
I0I0I0I@reddit (OP)
It is a BTRFS subvolume created by the Lubuntu installer. Here is the fstab that it created.
cknipe@reddit
Ok, so there are no files? Maybe the file got deleted but someone still has it open. In that case it won't go away until it's closed. People are talking about the swapfile and that does kinda make sense, but I see in the output of free your swap is at 0 so I don't think that's it. Try doing an "lsof -n | grep swap/" and see if anyone has an open file on that partition. If you don't have lsof handy I think "fuser -m /swap" should also give you some useful info.
I0I0I0I@reddit (OP)
I've rebooted. There are no files open.
The_Real_Grand_Nagus@reddit
My guess is there used to be a 16G file called /swap/swapfile
I0I0I0I@reddit (OP)
There was a file by that name, created by the Lubuntu installer, but it was only 512M. And that's not a swap partition. That's a BTRFS subvolume mounted on /swap.
The_Real_Grand_Nagus@reddit
I don't know what you're showing us. Did you check `lsblk` ? Have you checked `mount`? How about `ls /swap/` ? `swapon -a` ?
/swap on your system looks like a formatted mounted filesystem. Maybe it has swap files inside it? If the swap file(s) are 16G then that's what's taking up the space. It doesn't mean that anything is swapped out to disk.
I0I0I0I@reddit (OP)
/swap is a mounted BTRFS subvolume, where a 512M swapfile was created by the Lubuntu installer. That file has been removed. There is nothing else in /swap. The subvolume is empty. So, I want to know what's consuming 16G. That seems like an awful lot for metadata on an empty subvolume.
The_Real_Grand_Nagus@reddit
Check for snapshots:
sudo btrfs subvolume list /swap
Or open but deleted files:
sudo lsof +L1 /swap
I0I0I0I@reddit (OP)
https://www.reddit.com/r/linuxadmin/comments/1kyllbr/whats_using_space_in_swap/muykemh/
Tereza_packing_heat@reddit
BTRFS snapshots? List the btrfs subvolumes for me, please.
It should be noted that, every filesystem consumes a little even when empty. I dunno if 16G is the expected for a volume this size, but...
vogelke@reddit
If you have "top" installed, running top -w should show swap usage per process.
skreak@reddit
Swap is generally a partition - and _not_ a mounted filesystem. That 16gb could just be used inodes or other things. You just have 462gb partition that just so happens to be mounted to a folder called /swap - it has nothing to do, in this case, with swap memory. The output of swapon --verbose was empty so you don't have any swap partition or files set in /etc/fstab - you aren't using swap - /swap may as well just be called /foo.
I0I0I0I@reddit (OP)
It's a BTRFS subvolume that Lubuntu created for the purpose of holding a swapfile. The partition itself is not a swap file.
I0I0I0I@reddit (OP)
It's a BRTFS subvolume that the installer created and placed a 512M swap file on.
whetu@reddit
It used to be a rite of passage, for a newly minted *nix sysadmin, to write their own scripts to figure out which processes were doing what. They were often known as
cpuhogs
,memhogs
andswaphogs
respectively.So here's an old version of
swaphogs
from my code attic that you can try:I0I0I0I@reddit (OP)
Thanks! That's pretty nifty. It doesn't show anywhere near 16G usage though.
blin787@reddit
Why are you listing /swap/ folder? Why does it exist? Can you show us /etc/fstab ?
I0I0I0I@reddit (OP)
ralfD-@reddit
Wait, did you have a swap file instead of a swap partition? Did you delete that file? Unless you run `swapoff` before deleting (or rebotted) your kernel still has an open file handle for that swapfile even so you don't see it in file system listings.
I0I0I0I@reddit (OP)
Yes, Lubuntu installer created a BRTFS subvolume on /dev/sdb2 with a 512M swap file on it, called "swapfile". I've deleted that file, ran swapoff, then ran swapon on the two swap partiions.
NecroAssssin@reddit
I think a bigger question is why did you create a 462G swap partition?
I0I0I0I@reddit (OP)
I didn't.