Limiting WSL 2 RAM And CPU - gecko-8/devwiki GitHub Wiki

Up

  1. At the root of your C: drive (or whatever drive Windows boots from) add a .wslconfig file.
  2. Edit the file to contain the following (setting whatever RAM and CPU limits you want):
    [wsl2]
    memory=4GB # Limits VM memory in WSL 2 to 4 GB
    processors=2 # Makes the WSL 2 VM use two virtual processors
    
  3. Restart WSL from an admin PowerShell with the command:
    Restart-Service LxssManager
    
    NOTE: If running Docker, you will likely get an error asking to restart Docker.

Sources

https://medium.com/@lewwybogus/how-to-stop-wsl2-from-hogging-all-your-ram-with-docker-d7846b9c5b37