How-to's How to limit WSL memory usage
1. Create .wslconfig file with the following configuration
C:\Users\Development\.wslconfig
[wsl2]
memory=4GB
2. Restart the WSL service
  1. Restart-Service LxssManager
3. Verify the memory usage
Before
  1. free --human
              total        used        free      shared  buff/cache   available
Mem:          7.6Gi       1.7Gi       3.6Gi       0.0Ki       2.3Gi       5.7Gi
Swap:         2.0Gi       1.0Mi       2.0Gi
After
  1. free --human
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       105Mi       3.6Gi       0.0Ki       168Mi       3.5Gi
Swap:         1.0Gi          0B       1.0Gi