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
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. 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❯ View Expected Console Output
Opened Notepad for C:\Users<Username>.wslconfig. Confirm the target distribution uses VERSION 2 before applying these settings.
Step 2: Apply Recommended Production Tuning
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=trueStep 3: Restart the WSL Subsystem
Shutdown & 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❯ View Expected Console Output
[All WSL distributions stopped]
Launching default distribution…
Step 4: Verify Allocated Resources Inside Linux
Verify Updated Memory & Swap Caps
VerificationInside your Ubuntu/Debian WSL terminal, verify that the kernel respects the updated memory cap:
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