DEV Community

Cover image for Limiting Docker Memory Usage on Windows
Sarah Siqueira
Sarah Siqueira

Posted on • Edited on

Limiting Docker Memory Usage on Windows

As a WordPress developer, I used XAMPP for a long time, but for lots of reasons, some of which you can see in this other article: Why I changed from XAMPP to Docker, I decided to move to a Docker environment.

Turns out, the besides the learning curve to handle with Docker, and all other stuff, other problem came up. My poor Windows machine has become sluggish as it struggles to run other processes like Firefox and VS Code. It was basically impossible to work.

I opened up Task Manager while WSL 2 was running and noticed that the process named vmmem was absorbing a sizable chunk of my memory and slowing down my machine, turning impossible any other task.

What's it vmmem?

vmmem is a process that represents all the resources consumed by your Linux VM while WSL is running. According to Microsoft’s documentation, the amount of RAM that gets allocated to WSL depends on your Windows build number. On machines running build 20175 or higher, RAM is limited to either 50% of your total memory or 8GB, whichever happens to be smaller. On builds older than 20175, WSL could consume up to 80% of your host RAM.

While working on this article, I’m running a Windows 10 build 19044, so my WSL reserves 80% of my host RAM (8 GB). Fortunately, we can customize WSL 2 to limit the number of CPU cores, memory usage, swap space, and more. A big thanks to all the ones who replied in this GitHub issue pointing out how this can be done.

Workaround

To solve this, I follow the steps according to the Microsoft documentation.

Create a .wslconfig file

You need to create a .wslconfig file under your Windows user profile directory (C:\Users\YourUsername.wslconfig). On launch, WSL will read this file if it exists and configure itself accordingly. In this file, you can tell WSL 2 how much RAM, swap space, CPU cores, and other resources it should allocate.

I focused only on limiting memory usage, but you can configure any other options that you want, refer to the Microsoft docs for more options. The .wslconfig I am currently using is below (I don’t have a lot of RAM to work with):

[wsl2]
memory=2GB
Enter fullscreen mode Exit fullscreen mode

With just two lines, now I am able to run Docker on a machine with just 8 GB RAM. After restarting and launching docker-compose up again, my environment works perfectly without slowing down my machine.

Image description

Checking the Task Manager, vmmem does not consume more than 2 GB anymore. This is this my Docker environment which I use on daily basis working with WordPress and works perfectly with 2 GB RAM.

Top comments (2)

Collapse
 
dendihandian profile image
Dendi Handian

This is very important for windows user using docker. I could limit allocated ram resource into 500MB and all my alpine-based image and container still works

Collapse
 
sarahcssiqueira profile image
Sarah Siqueira

Yep, this it the environment I use on daily basis: github.com/sarahcssiqueira/docker-... and works perfectly with 2 GB :)