1

My disk filled up so I checked what was taking up all the space and Settings said that my Ubuntu distro for WSL was taking up 120GB as shown here:

enter image description here

Then I ran sudo ncdu / --exclude /mnt in WSL and it said it was only taking up 53.8GiB (57.8GB) of disk space.

Does anyone know what is causing this discrepancy in disk usage?

Markus Meyer
  • 1,614
TommoM
  • 13

1 Answers1

3

WSL2 works like a virtual machine; the guest OS doesn't directly store individual files in your host filesystem – it has a dedicated virtual "disk" that is stored in a single large .vhdx file as seen by Windows.

This file grows dynamically as the guest OS writes data to new "disk" areas, but does not shrink when those areas are no longer used. For example, if the virtual disk had sectors 10000-20000 in use by a large file and you removed it, the space for those sectors (and their data, too) is still consumed by the VHDX file.

So over time, as Ubuntu places new files all over the disk area, the VHDX will continue growing until it eventually reaches the complete "disk size" that was given to the VM.

Automatic compaction of the VHD used by WSL2 was actually added earlier this week.

If this were a dedicated Hyper-V VM, it would need to run periodic fstrim to inform the host OS of which VHD areas may be considered "free" (the same as what you'd use with SSDs); I suspect the new WSL2 feature works the same way, only performing the trim from its "system distro".

u1686_grawity
  • 452,512
  • I spotted that, but when I run wsl --manage Ubuntu-22.04 --set-sparse true it returns Invalid command line argument: --manage – TommoM Sep 21 '23 at 05:37
  • Have you actually upgraded to the latest WSL2 release? (And, well, I was assuming you're using WSL2 and not WSL1 because that's generally the default.) – u1686_grawity Sep 21 '23 at 05:41
  • Yes I am on WSL2. I also checked windows updates and ran wsl --update to install any updates that maybe had not installed. Apparently everything is up to date, but the above command gives the same output. – TommoM Sep 21 '23 at 06:02
  • WSL was installed from Windows Store or optional Windows Feature? Only the copy from Windows Store is updated – Ramhound Sep 21 '23 at 06:15
  • 2
    It turns out that the version of the WSL2 kernel that allows the above command is a pre-release. Hence, the it is only possible to update to that version using wsl --update --pre-release. After doing that I was able to run the above command successfully. – TommoM Sep 21 '23 at 07:38
  • I am just not sure how long it takes to kick in... – TommoM Sep 21 '23 at 07:39
  • Try manually running fstrim -av on your WSL2 (after fully restarting it). From the terminology ("sparse" VHDX as opposed to compaction), it sounds like the VHDX file will only reduce its physical size ("Size on Disk" in the Properties dialog) but will still show the same logical size, so while it will indeed free up space, I'm not entirely sure whether the Settings app will correctly report that. – u1686_grawity Sep 21 '23 at 07:49
  • That doesn't give any lines of output - I don't think it did anything – TommoM Sep 21 '23 at 07:57
  • I suspect that the feature which frees the disk space can only free memory when the files are deleted after the feature has been enabled. It probably isn't smart enough to compact the virtual hard drive by itself. I ended up compacting the virtual hard drive file with diskpart (which freed up 32GB) and then setting it to sparse. Hopefully this works moving forward. – TommoM Sep 21 '23 at 11:25