Surely, you have ever seen the Chrome and Android Studio memes about the huge consumption of ram and CPU.
Well... I think we have a new contender for the crown.
The King is dead, hail the new king.
Your majesty: WSL! đ
That is because have a known issue "WSL 2 consumes massive amounts of RAM and doesn't return it" and can't be solved until today.
Solution
It is not a definitive solution, but we can setup WSL with some advanced configurations to limit his resources.
All next steep need to be done on PowerShell (Windows side).
First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the wsl --shutdown
command.
Create a .wslconfig
file under the windows user profile path.
# PowerShell
cd $env:USERPROFILE
New-Item -Path '.wslconfig' -ItemType File
Open this file with VSCode (or any editor that you like)
# PowerShell
code .wslconfig
And set limits for the max amount of resources that can take (according to you needs). For example:
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=2
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB
# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=D:\\WSL\\wsl-swap.vhdx
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true
For more WSL advanced configuration check the documentation.
Save the file and restart WSL running wsl
command.
It will still consume the entire 4GBs regardless of Linux memory usage and only 2 virtual processors, but at least it will stop growing more than that.
Experimental
There is a new experimental version of WSL whit better managment of memory.
All next steps need to be done on PowerShell (Windows side).
Backup
First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the command.
# PowerShell
wsl --shutdown
Next, we are going to create a backup from our Ubuntu
linux distribution.
# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Don't worry if you do not see progress indicators. This can take a while depending on how much data you have. Maybe it's a good idea to take a rest.
When finished, first of all, and more important ensure that the D:\backup\ubuntu.tar
file exists.
Update
â ïž Beware that everything that was stored on the WSL drive can be lost, and the command won't ask for a confirmation, so make sure you backup everything you need first!
# PowerShell
wsl --update
wsl --update --pre-release
Config
Open again the .wslconfig
file with VSCode (or any editor that you like)
# PowerShell
code .wslconfig
And set the experimental flags (according to you needs). For example:
[experimental]
# Automatic memory reclaim
autoMemoryReclaim=gradual
# Automatic disk space clean up
sparseVhd=true
And finally restart WSL with wsl
or wsl --distribution <DistroName>
command.
Alternative
If you are still having issues with WSL, you can use Git Bash. It's not a "real" Linux distribution like WSL, but it's a good alternative to emulate an Unixish environment inside windows.
ZSH + Oh My ZSH! on Windows without WSL
Camilo Martinez ă» Oct 11 '22
Thatâs All Folks!
Happy Coding đ
Sources
Top comments (0)