How to Optimize WSL2 Memory & Disk Space (.wslconfig & VHDX Compaction)
Fix WSL2 memory leaks, reclaim gigabytes of SSD space from ext4.vhdx, and restrict RAM & CPU consumption using global .wslconfig settings in Windows 11.

Overview
Windows Subsystem for Linux 2 (WSL2) runs inside a lightweight Hyper-V virtual machine. By default, WSL2 dynamically expands its RAM usage up to 50% of your total system memory (or 8GB+) and expands its virtual disk (ext4.vhdx) up to 1TB. However, even when you delete files inside Linux, the VHDX file on Windows never automatically shrinks back down. In this guide, we show you how to configure resource caps and reclaim your wasted disk space.
Why Does WSL2 Consume So Much RAM & SSD Space?
- Linux Page Cache: Linux caches files in memory for speed. WSL2 retains this memory allocation unless explicitly configured to drop caches or auto-reclaim.
- Sparse Virtual Disk (VHDX): Deleting large Docker images or build directories inside Linux marks blocks as free inside Linux, but the host Windows .vhdx file remains expanded.
Step 1: Cap RAM & CPU Allocation via .wslconfig
You can strictly control how much RAM and CPU cores WSL2 is allowed to consume by creating a global .wslconfig file in your Windows user profile folder:
1. Press Win + R, type %USERPROFILE%, and press Enter.
2. Create a file named .wslconfig and paste the following configuration:
[wsl2]
memory=6GB # Caps RAM to 6GB (adjust based on system)
processors=4 # Uses 4 CPU cores maximum
swap=2GB # Allocates 2GB swap space
localhostForwarding=true
[experimental]
autoMemoryReclaim=dropcache # Automatically reclaims cached memory3. Save the file, open PowerShell as Administrator, and restart WSL2:
wsl --shutdownStep 2: Shrink and Compact ext4.vhdx Virtual Disk
To reclaim tens of gigabytes from a bloated ext4.vhdx file, follow these steps:
1. Shutdown all WSL instances from PowerShell:
wsl --shutdown2. Open Diskpart in Administrator PowerShell:
diskpart3. Select your distro's virtual hard disk file (replace Username and Distro with your actual paths):
select vdisk file="C:\Users\Username\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdiskTip: For WSL2 Kali Linux installations, the path is located inside %LOCALAPPDATA%\Packages\...\LocalState\ext4.vhdx. Check out our detailed wsl-kali-gui setup guide.
Step 3: Enable Auto Memory Reclaim in WSL2
In recent WSL2 updates, Microsoft added sparse VHDX mode and auto memory reclaim. You can enable automatic disk shrinking by running:
wsl --manage <DistroName> --set-sparse trueBest Practices & File System Performance
- Avoid working inside /mnt/c/: Always store active project files inside Linux home (~/projects) to prevent severe I/O cross-filesystem slowdowns.
- Clean Docker data routinely: Run docker system prune -a --volumes inside Linux to erase unused containers before running diskpart compact.
Summary & Key Takeaways
By combining a custom .wslconfig resource cap with diskpart vdisk compaction and sparse disk mode, you can eliminate WSL2 RAM slowdowns and free up over 30GB+ of SSD space.
Related Articles
Subscribe to the Newsletter
Get real-world IT troubleshooting guides, Azure VDI case studies, and sysadmin automation tips in your inbox.