WSL2 Performance Tuning & Memory Optimization
Before you begin
- Applies to:Windows hosts running WSL 2.
- Where to start:Start in Windows and review the existing per-user .wslconfig before editing it.
- What you need:Have the host's available memory and CPU in mind. Save work in all WSL distributions before applying changes because these settings affect all of them.
Guide tools
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:
Open Global .wslconfig in Windows Profile
ConfigurationThe .wslconfig file governs global settings across all WSL 2 distributions for this Windows user. It resides in your Windows user profile. If it already exists, back it up and merge these settings into its existing sections rather than replacing unrelated values. Check that the intended distribution is version 2 before tuning it:
# Open or create .wslconfig directly from PowerShellnotepad "$env:USERPROFILE\.wslconfig"
# Check the WSL package version and which distributions use WSL 2wsl --versionwsl --list --verbose❯ Illustrative Output (Results Vary)
Opened Notepad for C:\Users<Username>.wslconfig. Confirm the target distribution uses VERSION 2 before applying these settings.
Define Memory, CPU, and Mirrored Network Limits
TuningPaste 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 WSL2processors=4
# Configure swap space on your fastest SSDswap=4GB
# Windows 11 22H2 or later with a current WSL releasenetworkingMode=mirroreddnsTunneling=truefirewall=trueautoProxy=true
[experimental]# Gradually return Linux page-cache memory to WindowsautoMemoryReclaim=gradual
# Applies to newly created WSL VHDs; does not compact existing VHDssparseVhd=trueShutdown & Restart WSL Hyper-V Subsystem
ActivationTo commit the configuration changes, shut down the running Hyper-V microVM and restart WSL from PowerShell:
# This stops every running WSL distribution for this Windows user.wsl --shutdown
# Relaunch your default distributionwsl❯ Illustrative Output (Results Vary)
[All WSL distributions stopped]
Launching default distribution…
Verify Updated Memory & Swap Caps
VerificationInside your Ubuntu/Debian WSL terminal, verify that the kernel respects the updated memory cap:
free -h❯ Illustrative Output (Results Vary)
total used free shared buff/cache available
Mem: 7.8Gi 1.2Gi 5.8Gi 12Mi 850Mi 6.4Gi
Swap: 4.0Gi 0B 4.0Gi
Your feedback keeps these production procedures accurate and battle-tested.