How to Stop WSL2 from Hogging All Your Ram With Docker

Lew C
2 min readMay 24, 2020
Photo by Liam Briese on Unsplash

There’s no doubt about it, WSL2 (Windows Subsystem for Linux 2) has a host of performance benefits for everyone doing Linux related work on Windows. For me personally, it has significant performance benefits in Docker. My docker-compose usages feel snappier and my containers come up about 50% quicker.

The only downside of this, is that previously, I was using a Hyper-V image that ran my Docker engine. This is just how things work when using Docker on Windows. Of course, when you are using this solution, you can just limit the CPU’s and memory on the Docker virtual machine.

With WSL2 however, this explicitly doesn’t create a virtual machine that you can configure in Hyper-V. Instead, it does something else (I’m a little sketchy on what exactly, but it’s a more integrated experience than starting a virtual machine running Linux for your Docker containers).

The upside of this is that it’s a lot more performant. The downside is that in my experience, WSL2 will happily consume all your memory and CPU and pretty much starve out the host operating system, making all your Docker work really grindy and slow. The reason why is because you start your Docker containers on WSL2 through docker compose, and it seems like it just builds your images and hosts them while provisioning 100% of your computer’s resources to those environments. Pretty quickly your host OS runs out of RAM and so do your containers, and everything drags to a crawl. Fortunately it seems pretty easy to fix.

Setting boundaries for WSL2

We need to set some reasonable resource constraints on what WSL2 can actually use. Fortunately, that’s as simple as going to c:\users\*your your profile name* and creating a .wslconfig file. On my setup, a MSI Prestige 15 with a 10710u 6-core processor and 16GB of RAM, mine looks like this:

And then, from Powershell with admin rights, restart WSL2 by typing:

Restart-Service LxssManager

The vmmem process should now use a more reasonable amount of memory

Now, you should be able to use Docker on your Windows 10 machine without having it use every ounce of available resources.

Are you using WSL2 with your Docker install? Let me know it’s going for you in the comments.

Lew C

I code, and then I write about it.