WSL2 & Docker: Prevent Vmmem using too much memory
Have you seen this issue? Vmmem uses too much RAM, and your entire computer slows down to a crawl because it starts to affect the operating system as well. I found the solution.
This should work for Windows 10 and Windows 11, running WSL2 with Docker.
Windows hasn't fixed this problem yet, but Vmmem using too much memory is caused by WSL2 and Docker. There are various suggestions you can find online, including lowering how much memory Docker uses, but I found that those didn't actually fix the problem. It's not just Docker, but WSL2 and the VM using too much memory.
The issue is triggered by Docker using the WSL2 engine. But that is not something you want to undo, because it brings major performance improvements for Docker on Windows:
Instead, restrict how much memory WSL2 is allowed to use.
Press the Windows key to open Start menu, and type "powershell". Launch it:
best-match-windows-powershell.png
Shut down WSL so we can make changes:
wsl --shutdown
Type pwd
, and make sure you are in your C:\Users\<username>
directory. This is the default when you first open PowerShell.
Create a new file with notepad:
notepad .wslconfig
In the notepad window that appears, paste the following and then save:
[wsl2]
memory=4GB
processors=4
This will limit WSL2 so that it doesn't use so much memory, and limits memory to just 4 GB RAM and a total of 4 virtual processors.
Adjust these settings as you see appropriate. Don't put it too high. For a machine with 16 GB RAM, I found 4 GB dedicated to WSL2 is a good place to be.
Now restart WSL2:
wsl
You can now close the PowerShell window.
Type "docker" into your Start menu, and open Docker Desktop:
best-match-docker-desktop.png
Click the debugging icon in the top-right corner of the window:
debugging-icon.png
Restart Docker Desktop
restart-docker-desktop.png
You will now find that Vmmem limits itself to just 4 GB of memory at a time, but still runs smoothly otherwise. Problem solved!
This change shouldn't noticeably slow down your work, but it will prevent Vmmem from consuming too much memory and slowing down Windows.
For more information, refer to the website that I discovered the fix from, here.
I decided to rewrite it because I wanted to provide additional details and screenshots. And two articles with the solution to fixing Vmmem memory issues, are better than just one :]