Skip to content

WSL2 Performance Tuning & Memory Optimization

By default, WSL 2 can use up to 50% of Windows memory for its utility VM. This guide sets global resource limits and optional networking and memory-reclamation settings. Mirrored networking requires Windows 11 version 22H2 or later and a current WSL release; check your version before enabling it.

Architecture: Windows Host to WSL2 Resource Allocation

Configuring global resource caps ensures your Windows host maintains low latency while your Linux environment runs at peak performance:


Step 1: Open .wslconfig in Windows User Directory

01

Open Global .wslconfig in Windows Profile

Configuration

The .wslconfig file governs global settings across all WSL 2 distributions for this Windows user. It resides in your Windows user profile. Check that the intended distribution is version 2 before tuning it:

Terminal window
# Open or create .wslconfig directly from PowerShell
notepad "$env:USERPROFILE\.wslconfig"
# Check the WSL package version and which distributions use WSL 2
wsl --version
wsl --list --verbose
❯ View Expected Console Output

Opened Notepad for C:\Users<Username>.wslconfig. Confirm the target distribution uses VERSION 2 before applying these settings.


02

Define Memory, CPU, and Mirrored Network Limits

Tuning

Paste the settings that fit your machine into .wslconfig. The resource limits apply to every WSL 2 distribution for this Windows user. Mirrored networking changes how WSL connects to LANs and VPNs; use it only on supported Windows 11 systems. autoMemoryReclaim=gradual gradually releases cached Linux memory. sparseVhd=true marks newly created VHDs as sparse; it does not compact an existing distribution disk.

[wsl2]
# Cap memory allocation (e.g., 6GB on a 16GB machine, 16GB on a 32GB machine)
memory=8GB
# Limit CPU processors allocated to WSL2
processors=4
# Configure swap space on your fastest SSD
swap=4GB
# Windows 11 22H2 or later with a current WSL release
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
[experimental]
# Gradually return Linux page-cache memory to Windows
autoMemoryReclaim=gradual
# Applies to newly created WSL VHDs; does not compact existing VHDs
sparseVhd=true

Step 3: Restart the WSL Subsystem

03

Shutdown & Restart WSL Hyper-V Subsystem

Activation

To commit the configuration changes, shut down the running Hyper-V microVM and restart WSL from PowerShell:

Terminal window
# This stops every running WSL distribution for this Windows user.
wsl --shutdown
# Relaunch your default distribution
wsl
❯ View Expected Console Output

[All WSL distributions stopped]
Launching default distribution…


Step 4: Verify Allocated Resources Inside Linux

04

Verify Updated Memory & Swap Caps

Verification

Inside your Ubuntu/Debian WSL terminal, verify that the kernel respects the updated memory cap:

Terminal window
free -h
❯ View Expected Console Output

total used free shared buff/cache available
Mem: 7.8Gi 1.2Gi 5.8Gi 12Mi 850Mi 6.4Gi
Swap: 4.0Gi 0B 4.0Gi

Comments