5

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,910
TommoM
  • 53

1 Answers1

5

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".

grawity
  • 501,077