By default, WSL 2 may take up to half of your PC's memory - and on some builds far more - before it thinks about giving any back. One small text file with three lines puts you back in charge of your own machine.
The symptom is familiar: a few containers running, and the whole PC turns sluggish because a process called Vmmem is sitting on gigabytes of RAM. Vmmem is the WSL 2 virtual machine, and Docker's engine lives inside it. WSL 2 grows its memory as guests demand it and is lazy about returning it. The fix is not a registry hack - it is Microsoft's own settings file, .wslconfig, and this page shows exactly what to put in it.
.wslconfig.wslconfig.txt - in the Save dialog, set the type to All FilesC:\Users\YourName\.wslconfig - it is per-user and applies to every WSL distro and to Docker's engine togetherEverything sits under a [wsl2] heading:
memory=4GB - the maximum RAM the whole WSL 2 VM may use. On a 16 GB machine, 4 to 6 GB is a sane Docker allowance; on an 8 GB machine, start at 3 to 4 GBprocessors=2 - how many of your CPU cores the VM may use. Half your cores is a reasonable ceilingswap=2GB - the overflow file WSL uses when the memory cap is hit. Some swap prevents hard crashes; too much just moves the slowness to diskHonest framing: these are ceilings, not reservations. WSL still only uses what it needs - you are setting the maximum damage, not pre-paying memory.
wsl --shutdown - this fully stops the WSL 2 VM. Editing the file without this does nothing, which is the source of most 'wslconfig does not work' complaintsA cap set too low does not crash Windows - it crashes your containers, with out-of-memory kills that look like application bugs. If containers start dying after you add limits, raise memory a step at a time. Match the cap to the work: one small database container is happy in 2 GB; a build pipeline or a local AI model is not, and pretending otherwise just moves the failure somewhere stranger.
Watch the video to get Docker running first - then use this file to keep it polite on a shared machine.
▶️ Watch Full VideoAlmost always because the running VM never restarted. The file is only read when WSL 2 starts. Quit Docker Desktop, run wsl --shutdown, wait a few seconds, then start things again. The second cause is the classic one: Notepad saved the file as .wslconfig.txt - check the real filename in Explorer with file extensions visible.
Start with memory=4GB or 6GB for general development. That leaves Windows and your browser comfortable while giving Docker real room. If you build large images or run several services together, raise it - the right number is the one where your containers stop being killed and your PC stays responsive.
All of it. The settings apply to the single WSL 2 virtual machine that hosts every distro and the Docker engine together. There is no per-distro memory cap in this file - the ceiling is shared, which is worth remembering when Ubuntu and Docker seem to be competing.