TechBuzzPro

Set WSL Memory, CPU, Disk and Swap Limits on Windows

· 5 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Create or edit the .wslconfig file. Open a text editor as Administrator and create a file named .wslconfig in your user profile folder (e.g., C:\Users\YourName\.wslconfig). If the file already exists, open it for editing.
  2. 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. The memory and swap values accept KB, MB, or GB suffixes.
  3. 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.
  4. 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.
  5. 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.
    1. Export the current distro to a tarball:
      wsl --export DistroName C:\temp\distro.tar
    2. Unregister the existing distro (make sure you have a backup):
      wsl --unregister DistroName
    3. 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-size flag creates a fixed‑size virtual disk. Replace 20GB with whatever limit you need.
    After the import, start the distro again and verify the available space with df -h.
  6. Fine‑tune swap location (optional). If you prefer the swap file on a faster SSD or a separate drive, change the swapFile path in .wslconfig and repeat step 3 to apply.
  7. Persist the changes across Windows updates. The .wslconfig file remains in your user profile, so future updates will respect the limits unless you delete or modify the file.

Common Mistakes

  • Saving .wslconfig in the wrong folder (it must be directly under C:\Users\YourName, not in Documents or Desktop).
  • Using incorrect units (e.g., typing memory=4096 without a suffix defaults to bytes, effectively allocating no RAM).
  • Forgetting to run wsl --shutdown after editing the file, which leaves the old limits in effect.
  • Attempting to set a disk size with .wslconfig – disk limits are managed via wsl --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 .wslconfig file 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 vmmem process, but the processors entry in .wslconfig is 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:

  1. Restart Windows. Some changes, especially swap file location, require a full reboot to clear cached VMMEM state.
  2. Confirm the file location. Open PowerShell and run Get-Content $env:USERPROFILE\.wslconfig to ensure the file contains the exact syntax you edited.
  3. Check Windows version. Run winver and verify you are on at least build 19041, which introduced full .wslconfig support.
  4. 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.
  5. Verify that the distro is using WSL 2. Run wsl -l -v. If it shows version 1, the .wslconfig limits do not apply. Convert with wsl --set-version DistroName 2.
  6. Inspect the VMMEM process. Open Task Manager, locate vmmem, and check its memory usage. If it exceeds your limit, the config was not loaded.
  7. 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.
  8. 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.