How to Stop WSL2 from Hogging All Your Ram With Docker

I have been doing some performance testing using Docker, Docker-Compose. I was running into an issue, my Dev laptop was showing that Docker (vmmem) was using 90% of my system RAM. That is not great, nor what I want when running performance tests. Several times during my tests my system literally crashed and died :).

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.

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. My observation, 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 Dell Precision 5550, i7-10750H CPU @ 2.6GHz, 1 socket, 6 cores, 12 logical processors with 32 GB RAM mine looks like this:

[wsl2]
memory=4GB # Limits VM memory in WSL 2 to 4 GB
processors=5 # Makes the WSL 2 VM use two virtual processors

Now you can restart docker. After a full restart of Docker, let’s checkout the Vmmem process.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.