WSL - Insufficient System Resources Even There is Plenty?

Sometime when I open the WSL terminal, it said Insufficient system resources exist to complete the requested service. and refuse to function. Buy hey, my computer is still having nearly 8GB of free memory. There is no way that it is not enough!

It turns out that WSL default setting is assigning "min(50% of total memory, 8GB)" memory to the WSL 2 VM. Seem that WSL simply refuse to launch if there is not enough memory even it only use a few MB when idle. To change this setting, you can create a .wslconfig file with the following:

# 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

The config file should be placed in the %UserProfile% directory. In case you don't know where it is, open command prompt and run echo %UserProfile%. Now, you should able to use the WSL without facing that problem like me again.

Reference

https://learn.microsoft.com/en-us/windows/wsl/wsl-config