WSL Tepes: The CPU/Memory vampire
Tags: terminal, wsl, linux, ubuntu
Published: 2023-03-03 20:33:28

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32bog0qn3osdfjbbmvu0.png)

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](https://github.com/microsoft/WSL/issues/4166) _"WSL 2 consumes massive amounts of RAM and doesn't return it"_ and can't be solved until today.

![not cool](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttfr3xo12goumqmt5c4k.png)

---

## Solution

It is not a definitive solution, but we can setup WSL with some [advanced configurations](
https://learn.microsoft.com/en-us/windows/wsl/wsl-config#example-wslconfig-file) 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.

```bash
# PowerShell
cd $env:USERPROFILE
New-Item -Path '.wslconfig' -ItemType File
```

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

```bash
# PowerShell
code .wslconfig
```

And set limits for the max amount of resources that can take (according to you needs). For example:

```bash
# 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](https://learn.microsoft.com/en-us/windows/wsl/wsl-config) check the documentation.

Save the file and restart WSL running `wsl` command.

![htop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th852a0ik31lwe9ky82r.png)

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.

```bash
# PowerShell
wsl --shutdown
```

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

```bash
# 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.

![waiting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inlo80w5ccpkaxjcnv4d.gif)

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!

```bash
# PowerShell
wsl --update
wsl --update --pre-release
```

### Config

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

```bash
# PowerShell
code .wslconfig
```

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

```bash
[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.

{% post https://dev.to/equiman/zsh-on-windows-without-wsl-4ah9 %}

---

**That’s All Folks!
Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)

> **Sources**
> - [Windows Subsystem for Linux September 2023 update](https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/)