Limit the memory usage in Windows 10 WSL2
Limit the memory usage
It was annoying that WSL2 was always eating up my poor RAM. The following solution worked. [Reference]
My distribution is:
1 | > wsl -l -v |
Create a configure file named .wslconfig
in your user
directory in Windows.
Open PowerShell as administrator, shutdown WSL2 and edit the file:
1 | > wsl --shutdown |
For me the content is:
1 | [wsl2] |
After these steps, the memory usage of my WSL2 never bothered me again.
Compact the virtual disk
This file always occupies a huge amount of space:
%USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_*\LocalState\ext4.vhdx
.
It's because WSL2 won't actually release the space after files are
deleted. The solution is to compact the virtual disk to reduce its size
as follows. [Reference]
Open PowerShell as Administrator:
1 | > wsl --shutdown |
Press Ctrl+C
to exit DISKPART
.
P.S. This didn't work for me (optimize-vhd
is not
found):
1 | > optimize-vhd -Path .\ext4.vhdx -Mode full |