Blog

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.

August 2026·6 min read·Virtualization
How to Optimize WSL2 Memory & Disk Space (.wslconfig & VHDX Compaction)

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:

ini
[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 memory

3. Save the file, open PowerShell as Administrator, and restart WSL2:

powershell
wsl --shutdown

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

powershell
wsl --shutdown

2. Open Diskpart in Administrator PowerShell:

powershell
diskpart

3. Select your distro's virtual hard disk file (replace Username and Distro with your actual paths):

powershell
select vdisk file="C:\Users\Username\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk
Tip

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

powershell
wsl --manage <DistroName> --set-sparse true

Best 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.

Subscribe to the Newsletter

Get real-world IT troubleshooting guides, Azure VDI case studies, and sysadmin automation tips in your inbox.

Strict Zero-Spam Promise: Used solely to send technical notes and updates. Never shared, sold, or rented. Complete permanent data deletion upon 1-click unsubscribe. Read our Privacy Policy.

rinkurapria.devAn enterprise IT portfolio & sysadmin registry.
Crafted By@Rapria_Rinku
Source CodeGitHub
StackNext.js · Tailwind CSS
LicenseMIT License
© 2026 Rinku Rapria · Endpoint Engineer
[ENTERPRISE ENDPOINT ARCHITECTURE]
RINKURAPRIA
SYS·NODE: 28.6139° N, 77.2090° E·PROTOCOL: HTTPS/3 QUIC