How to Limit WSL 2 Memory and CPU Usage

🐧Part of the WSL Complete Guide, every Windows Subsystem for Linux post on this site, organised by task.
The short answer

Create %UserProfile%\.wslconfig, set memory= and processors= under a [wsl2] section, then run wsl --shutdown and wait about eight seconds.

WSL 2 runs in a lightweight virtual machine, and by default that machine is allowed a large share of your total RAM. On a laptop that is also running Docker Desktop, SSMS and a browser, you notice. The fix is a small config file that most people never find out about.

Before you set limits, check what WSL has now. With no .wslconfig it takes about half of the machine’s memory and every logical processor. On my 8GB machine that is 3.7GB and 8 processors:

PowerShell
wsl free -h
wsl nproc

Before I set a limit, WSL on my 8 GB laptop had 3.7 GB of memory, about half the RAM, which is the default, and all 8 processors:

free -h and nproc inside WSL before a limit is set: 3.7 GB of memory and 8 processors on an 8 GB laptop
wsl free -h showing 3.7Gi of memory and 1.0Gi of swap, and wsl nproc returning 8, on a machine with no .wslconfig

Create the Config File

The file lives in your Windows user folder, not inside the distribution:

PowerShell
# create or open it
notepad "$env:USERPROFILE\.wslconfig"

A sensible starting point for a 16GB machine:

Text
[wsl2]
memory=4GB
processors=4
swap=2GB

# optional: turn the swap file off entirely
# swap=0

Apply It

PowerShell
# stop the WSL virtual machine so the new settings load
wsl --shutdown

# wait ~8 seconds, then start WSL as normal
wsl

The wait matters. WSL keeps the virtual machine alive briefly after the last distribution stops, and starting again too quickly reuses the old settings. Microsoft documents roughly eight seconds.

Check It Worked

From inside any distribution:

Text
free -h
Text
              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.2G        2.0G       90.0M      517.1M        2.3G

The total figure should now reflect your limit rather than most of the machine.

Set it too low and things fail in confusing ways. Builds get killed, Docker containers exit without a clear reason, and databases refuse to start. If something that used to work stops working after this change, raise the limit before looking anywhere else.

Not sure what numbers to pick? Tell Claude how much RAM the machine has and what else runs on it, and ask for a starting .wslconfig. Then check the result with free -h as above.

Related


If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *