I've switched all of my Docker and *nix usage to use WSL 2 for a while now, and largely have been free of any issues/problems. That is, until recently, when I've been working on speeding up my team's production Docker image builds (another story). This work heavily involved modifying
Dockerfile commands, trying out different approaches for building Angular apps to compare performance.
Evidently restoring and building Angular apps is one of the most resource intensive operations one can perform in technology, because after a day or two of this effort, my machine ground to a halt when not performing a build. Ultimately, I hit two limits:
- Disk space
WSL 2, much like Chrome, will consume all available resources until your machine begins to smoke. Unlike Chrome, WSL 2 will not release these resources, sometimes even after a reboot.
The RAM issue is rather straightforward to solve, just create a
.wslconfig file in the root of your user's home directory and configure it to not consume so much:
My machine has 32GB so half to WSL 2 seemed reasonable. Disk space, unfortunately, was much harder.
Taming WSL 2 Disk Space
I first saw a problem when trying to build some Nx-based example apps:
error Could not write file "C:\\src\\nrwl\\nx-examples\\yarn-error.log": "ENOSPC: no space left on device, write"
Uh oh. Maybe this is a mistake. Let's check the space:
Free space hovered around 1GB, but would get much lower then larger as the pagefile struggled to keep up. That's not good.
I ran the normal "disk cleanup" which would net me a whopping 80MB back:
When this fails, I reach for the trusty WinDirStat tool to see what we see. To my surprise, there was one ENORMOUS file filling up my drive:
After some searching, it turns out that this file is the partition that WSL 2 uses. Great, can I limit this in the settings? Nope! Just memory and CPU. However, there is a l-o-o-o-o-n-g GitHub issue that has some potential fixes. The most popular answer was to shutdown WSL and compact the disk using the
wsl --shutdown optimize-vhd -Path .\ext4.vhdx -Mode full
Well, that did basically nothing for me. I got maybe a hundred MB back from that. OK what if we clear out the unused images in Docker? I go into Docker Desktop and clean up unused images:
That's odd, a size much much lower than the file size that Docker Desktop says are being used? Time for the nuclear option, go into "Troubleshoot -> Clean / Purge Data":
FINALLY after purging the WSL 2 data set, does my disk space get back to normal, back to "only" 2GB:
I don't see a way to permanently fix this, other than to keep an eye on it and purge when it gets to big. 🤷♂️