Limit WSL2 Memory and CPU Usage in Windows 11

Windows Subsystem for Linux 2 (WSL2) can dynamically consume up to 50% of your total system memory or more by default, which can cause performance bottlenecks on your host Windows 11 system. This guide provides a straightforward method to limit WSL2’s RAM allocation and CPU core usage by creating and modifying the global .wslconfig configuration file.

Step 1: Open Your User Profile Directory

The .wslconfig file controls global settings across all installed WSL2 distributions and must be placed in your Windows user profile root folder.

  1. Press Win + R on your keyboard to open the Run dialog.
  2. Type %USERPROFILE% and press Enter. This opens C:\Users\<YourUsername>.

Step 2: Create or Edit the .wslconfig File

  1. Check if a file named .wslconfig already exists in this folder.
  2. If it does not exist, right-click inside the directory, choose New > Text Document, and name it .wslconfig (ensure you remove the .txt file extension).
  3. Open .wslconfig in Notepad or any code editor.

Step 3: Add Memory and CPU Limits

Paste the following configuration into the .wslconfig file:

[wsl2]
# Limit the maximum memory allocated to WSL2 (e.g., 4GB, 8GB, 16GB)
memory=4GB

# Limit the number of logical processor cores WSL2 can use
processors=2

# Optional: Set the size of the swap virtual memory file
swap=2GB

Configuration Parameters Explained:

Save and close the file after making your changes.

Step 4: Restart WSL2 to Apply Changes

For the new resource limits to take effect, you must restart the WSL service:

  1. Open PowerShell or Command Prompt as an Administrator.

  2. Run the shutdown command:

    wsl --shutdown
  3. Relaunch your Linux distribution from the Start menu or terminal.

Step 5: Verify the Applied Limits

Open your WSL2 terminal and run the following commands to ensure the limits are active:

The outputs should match the values configured in your .wslconfig file.