DEV Community

Cover image for WSL Tepes: The CPU/Memory vampire
Camilo Martinez
Camilo Martinez

Posted on • Edited on

WSL Tepes: The CPU/Memory vampire

Surely, you have ever seen the Chrome and Android Studio memes about the huge consumption of ram and CPU.

Image description

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.

not cool


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


Enter fullscreen mode Exit fullscreen mode

Open this file with VSCode (or any editor that you like)



# PowerShell
code .wslconfig


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

For more WSL advanced configuration check the documentation.

Save the file and restart WSL running wsl command.

htop

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


Enter fullscreen mode Exit fullscreen mode

Next, we are going to create a backup from our Ubuntu linux distribution.



# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar


Enter fullscreen mode Exit fullscreen mode

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.

waiting

When finished, first of all, and more important ensure that the D:\backup\ubuntu.tarfile 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


Enter fullscreen mode Exit fullscreen mode

Config

Open again the .wslconfig file with VSCode (or any editor that you like)



# PowerShell
code .wslconfig


Enter fullscreen mode Exit fullscreen mode

And set the experimental flags (according to you needs). For example:



[experimental]

# Automatic memory reclaim
autoMemoryReclaim=gradual

# Automatic disk space clean up
sparseVhd=true


Enter fullscreen mode Exit fullscreen mode

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.


That’s All Folks!
Happy Coding
🖖

beer

Sources

Top comments (0)