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.

Create the Config File

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

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

A sensible starting point for a 16GB machine:

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

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

Apply It

# 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:

free -h
              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.

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 *