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.
- Press
Win + Ron your keyboard to open the Run dialog. - Type
%USERPROFILE%and press Enter. This opensC:\Users\<YourUsername>.
Step 2: Create or Edit the .wslconfig File
- Check if a file named
.wslconfigalready exists in this folder. - If it does not exist, right-click inside the directory, choose
New > Text Document, and name it
.wslconfig(ensure you remove the.txtfile extension). - Open
.wslconfigin 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=2GBConfiguration Parameters Explained:
- memory: Defines the maximum amount of RAM the WSL2
virtual machine can consume. You can specify values in gigabytes (e.g.,
8GB) or megabytes (e.g.,4096MB). - processors: Sets the number of CPU cores available to WSL2. Enter a whole integer that is less than or equal to your total logical processors.
- swap: Sets the swap storage limit. Setting this prevents WSL2 from creating an excessively large swap file on your disk.
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:
Open PowerShell or Command Prompt as an Administrator.
Run the shutdown command:
wsl --shutdownRelaunch 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:
Check assigned memory:
free -hCheck assigned CPU cores:
nproc
The outputs should match the values configured in your
.wslconfig file.