Limiting WSL 2 Memory Usage to Prevent Vmmem Resource Exhaustion
If you use Windows Subsystem for Linux (WSL 2) for development, you've likely noticed a process called Vmmem taking up a massive amount of RAM in Task Manager.
By default, WSL 2 can consume up to 80% of your total system memory. While this is great for performance within Linux, it can leave your Windows environment feeling sluggish, especially if you're running memory-heavy tools like Docker, IDEs, and multiple browser tabs simultaneously.
The Problem: The Memory Hog
WSL 2 uses a lightweight utility VM. Unlike traditional VMs, it doesn't always release memory back to the host OS immediately after the Linux processes have finished. This led to a well-known issue in the WSL repository where users report Vmmem consuming gigabytes of RAM even when WSL appears idle.
The Solution: .wslconfig
Fortunately, Microsoft provides a way to globally configure WSL 2 settings via a file called .wslconfig.
1. Create the configuration file
Open your Windows User Profile directory (you can type %UserProfile% in the File Explorer address bar). Look for a file named .wslconfig. If it doesn't exist, create a new text file and name it exactly .wslconfig (ensure there is no .txt extension).
2. Add memory limits
Open the file in a text editor like Notepad or VS Code and add the following configuration:
[wsl2]
# Limit the memory used by the WSL 2 VM to 4GB (adjust as needed)
memory=4GB
# Limit the number of virtual processors to 2
processors=2
# Enable or disable swap (optional)
swap=8GB
3. Restart WSL
For these changes to take effect, you must shut down WSL completely. Open PowerShell or a Command Prompt and run:
wsl --shutdown
The next time you open a WSL terminal, the Vmmem process will be constrained by the limits you defined.
Which Limit Should You Choose?
A common recommendation is to set the memory limit to 50% of your system RAM or a fixed value like 4GB or 8GB, depending on your specific needs (e.g., if you are running large Kubernetes clusters or heavy compilation tasks).
Conclusion
By taking a few minutes to configure .wslconfig, you can prevent WSL 2 from starving your host machine of resources, ensuring a smoother development experience on both Windows and Linux.