Limit WSL memory usage.

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

When using WSL2, the Linux subsystem will continuously allocate system memory while running, as long as there is available memory in the system. As it continues to run, it will occupy more and more memory, and may even fill up the memory of the Windows system. Therefore, it is necessary to limit its memory usage when running WSL.

The operation process is as follows:

  1. Create a .wslconfig configuration file in the user directory.

    • Open the command line.
    • Enter the command notepad .wslconfig to create and edit the configuration file.
  2. Write the WSL configuration:

    1
    2
    3
    4
    [wsl2]
    memory=2GB
    swap=8GB
    localhostForwarding=true

  3. Restart WSL using wsl --shutdown.