How to add memory limit consumption for Docker/Vmmem/WSL2

Hendi Suhardja
2 min readJul 6, 2021

--

Windows Subsystem for Linux (WSL) 2 introduces a significant architectural change as it is a full Linux kernel built by Microsoft, allowing Linux containers to run natively without emulation. With Docker Desktop running on WSL 2, users can leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts. In addition, WSL 2 provides improvements to file system sharing, boot time, and allows access to some cool new features for Docker Desktop users.

Docker Desktop uses the dynamic memory allocation feature in WSL 2 to greatly improve the resource consumption. This means, Docker Desktop only uses the required amount of CPU and memory resources it needs, while enabling CPU and memory-intensive tasks such as building a container to run much faster.

But with this , when I install docker, I found my RAM consumption is very huge, it always take above 96% of my RAM, then it will make your machine in slow performance

Solution

So to fix this issue, we need to limit the usage of VmMem, we could do this by following these steps :

  • Open window explorer, navigate to %userprofile%, this shortcut will navigate you to your user folder
  • Create a file with name “.wslconfig”
  • Open this file using any text editor, then paste below configuration, you could change the memory (7GB) depends on your RAM capacity

[wsl2]
memory=7GB # Limits VM memory in WSL 2 to 7 GB

  • Open your Powershell with admin credential, then execute below command to restart WSL2

Restart-Service LxssManager

Now your memory consumption for WSL2 will be limited, and this service will not consume all of your memory anymore

Thanks for reading

--

--