Limiting WSL2 memory consumption

Just a quick note about WSL2. It might consume a lot of RAM and eventually cause blue screens in Windows 10. If you experience something like this, just follow this process:

  1. Shutdown the wsl instances
    wsl --shutdown
  2. create the .wslconfig file under c:\Users\<username>
[wsl2]
memory=4GB   # Limits VM memory in WSL 2 up to 3GB
processors=4 # Makes the WSL 2 VM use two virtual processors

This configuration file supports the following syntax as described here

[wsl2]
kernel=<path>              # An absolute Windows path to a custom Linux kernel.
memory=<size>              # How much memory to assign to the WSL2 VM.
processors=<number>        # How many processors to assign to the WSL2 VM.
swap=<size>                # How much swap space to add to the WSL2 VM. 0 for no swap file.
swapFile=<path>            # An absolute Windows path to the swap vhd.
localhostForwarding=<bool> # Boolean specifying if ports bound to wildcard or localhost in the WSL2 VM should be connectable from the host via localhost:port (default true).

# <path> entries must be absolute Windows paths with escaped backslashes, for example C:\\Users\\Ben\\kernel
# <size> entries must be size followed by unit, for example 8GB or 512MB