Set WSL Memory, CPU, Disk and Swap Limits on Windows
In this article
Yes—you can control how much RAM, how many CPU cores, how much swap space, and even the virtual disk size that a WSL 2 distribution uses by editing the .wslconfig file and using a few wsl.exe commands. This lets you keep WSL from hogging resources on a laptop, enforce consistency across multiple machines, or simply match the limits of a low‑end development environment.
What You'll Need
- Windows 10 version 1903 or later (or Windows 11) with the WSL 2 optional component enabled.
- A text editor with administrative privileges (Notepad, VS Code, etc.).
- PowerShell or Command Prompt opened as Administrator.
- At least one WSL 2 distribution already installed (Ubuntu, Debian, etc.).
- Basic familiarity with file paths (e.g., C:\Users\YourName).
Step-by-Step
- Create or edit the .wslconfig file. Open a text editor as Administrator and create a file named
.wslconfigin your user profile folder (e.g.,C:\Users\YourName\.wslconfig). If the file already exists, open it for editing. - Define the resource limits. Add a
[wsl2]section and specify the desired values. Example:[wsl2] memory=4GB # Limits RAM to 4 GB processors=2 # Allows two logical CPUs swap=2GB # Creates a 2 GB swap file swapFile=C:\\temp\\swap.vhdx # Optional custom swap location localhostForwarding=true
Adjust the numbers to suit your hardware. Thememoryandswapvalues acceptKB,MB, orGBsuffixes. - Save the file and restart WSL. After saving, open an elevated PowerShell window and run:
wsl --shutdown
This stops all running distributions and forces them to reload the new configuration. - Verify the limits. Start your distro and run the following commands inside the Linux shell:
cat /proc/meminfo | grep MemTotal # Shows the RAM limit nproc # Shows the CPU core count
The reported values should match what you set in.wslconfig. - Adjust virtual disk size (optional). WSL 2 stores its filesystem in a VHDX file. To shrink or expand it, you need to re‑import the distro with a size argument.
- Export the current distro to a tarball:
wsl --export DistroName C:\temp\distro.tar
- Unregister the existing distro (make sure you have a backup):
wsl --unregister DistroName
- Re‑import with a custom VHD size (e.g., 20 GB):
wsl --import DistroName C:\WSL\DistroName C:\temp\distro.tar --version 2 --vhd C:\WSL\DistroName\ext4.vhdx --vhd-size 20GB
The--vhd-sizeflag creates a fixed‑size virtual disk. Replace20GBwith whatever limit you need.
df -h. - Export the current distro to a tarball:
- Fine‑tune swap location (optional). If you prefer the swap file on a faster SSD or a separate drive, change the
swapFilepath in.wslconfigand repeat step 3 to apply. - Persist the changes across Windows updates. The
.wslconfigfile remains in your user profile, so future updates will respect the limits unless you delete or modify the file.
Common Mistakes
- Saving
.wslconfigin the wrong folder (it must be directly underC:\Users\YourName, not in Documents or Desktop). - Using incorrect units (e.g., typing
memory=4096without a suffix defaults to bytes, effectively allocating no RAM). - Forgetting to run
wsl --shutdownafter editing the file, which leaves the old limits in effect. - Attempting to set a disk size with
.wslconfig– disk limits are managed viawsl --import, not the config file. - Specifying a swap file path on a drive that does not exist or is read‑only, causing WSL to fail on start.
Tips
- Keep the
.wslconfigfile version‑controlled if you share the same environment across multiple machines. - Use powers of two for memory and swap (e.g., 2GB, 4GB) to avoid rounding issues in older Windows builds.
- If you need more granular CPU control, you can also use the Windows Task Manager to set an affinity for the
vmmemprocess, but theprocessorsentry in.wslconfigis the recommended method. - When resizing the VHD, choose a size slightly larger than the expected maximum to avoid running out of space during package installations.
- Regularly run
wsl --status(available in newer builds) to see the active configuration and verify that Windows recognized your limits.
Troubleshooting
If WSL does not respect the limits you set, follow these checks in order:
- Restart Windows. Some changes, especially swap file location, require a full reboot to clear cached VMMEM state.
- Confirm the file location. Open PowerShell and run
Get-Content $env:USERPROFILE\.wslconfigto ensure the file contains the exact syntax you edited. - Check Windows version. Run
winverand verify you are on at least build 19041, which introduced full .wslconfig support. - Look for syntax errors. Even a stray space before a key (e.g.,
memory=2GB) will cause Windows to ignore the entire section. Remove leading spaces. - Verify that the distro is using WSL 2. Run
wsl -l -v. If it shows version 1, the .wslconfig limits do not apply. Convert withwsl --set-version DistroName 2. - Inspect the VMMEM process. Open Task Manager, locate
vmmem, and check its memory usage. If it exceeds your limit, the config was not loaded. - Re‑import the distro for disk size changes. The only reliable way to enforce a hard disk cap is to re‑import as described in step 5 of the main guide.
- Delete corrupted swap files. If WSL fails to start after changing
swapFile, delete the old swap VHDX at the previous path and restart WSL.
If none of the above resolves the issue, consider resetting WSL entirely (this removes all distributions): wsl --unregister DistroName, then reinstall the distro from the Microsoft Store. Back up any important data first.
Frequently Asked Questions
Can I set a hard limit on the WSL virtual hard disk without re‑importing?
No. Disk size is controlled by the VHDX file itself. Changing the limit requires exporting, unregistering, and re‑importing the distro with the --vhd-size argument.
Do the limits apply to all WSL distributions on the machine?
Yes. The .wslconfig file is global; every WSL 2 distro reads the same settings when it starts.
Is there a way to limit network bandwidth for a WSL distro?
WSL does not provide a built‑in bandwidth limiter. You would need to use external Windows tools such as PowerShell’s Set-NetQosPolicy or third‑party traffic shapers.
What happens if I set the swap size larger than the physical RAM?
Windows will create a swap file of the requested size, but performance may degrade heavily because the system will start paging to disk more often. It is generally advisable to keep swap below or equal to RAM.
Can I change the limits on the fly without restarting WSL?
No. The .wslconfig file is read only when the WSL VM boots. You must run wsl --shutdown (or reboot) for changes to take effect.
Do these settings affect Windows containers or Docker Desktop?
Docker Desktop runs its own WSL 2 VM separate from the default distro, and it manages resources via its own settings UI. Changing .wslconfig will not affect Docker’s allocations.