Optimizing WSL2 Performance: Setting Memory and CPU Allocation on Windows

Geronimo
2 min readNov 24, 2023

Welcome to a quick guide on optimizing your Windows Subsystem for Linux 2 (WSL2) experience by customizing its memory and CPU usage. This tutorial is perfect for those who are looking to enhance their WSL2 performance on a Windows system.

Introduction

WSL2 offers a powerful platform for running a Linux environment directly on Windows. However, to get the most out of it, you might want to fine-tune its resource usage, particularly memory and CPU allocations. Here’s how you can do it.

Step 1: Creating the .wslconfig File

The configuration of WSL2 is managed through a file named .wslconfig. This file doesn’t exist by default, so you’ll need to create it. Here's how:

  1. Open File Explorer and navigate to your user folder (usually C:\Users\[YourUsername]).
  2. Open Notepad or your preferred text editor.
  3. Create a new file and save it as .wslconfig in your user folder. Ensure the file name includes the leading period.

Step 2: Editing the .wslconfig File

With your .wslconfig file created, it's time to specify your desired settings. Here’s an example configuration:

[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

In this example, we’re setting WSL2 to use a maximum of 4 GB of memory and 2 CPU cores. Feel free to adjust these numbers according to your system’s capabilities and your requirements. For instance, if you have more RAM and want to allocate 64 GB and 8 processors, your file would look like this:

[wsl2]
# Limits VM memory to use no more than 64 GB
memory=64GB
# Sets the VM to use eight virtual processors
processors=8

Step 3: Applying Your Configuration

After saving your .wslconfig file with the desired settings, you need to restart WSL2 for the changes to take effect. Here's how to do it:

  1. Open Command Prompt or PowerShell.
  2. Run the command: wsl --shutdown. This will shut down the WSL2 VM.
  3. Start WSL2 again by opening your preferred Linux distribution from the Start Menu, or simply run the command: wsl.

Your WSL2 instance will now start with the new memory and CPU settings applied.

Conclusion

Customizing your WSL2 setup can significantly improve your development experience on Windows, especially when working on resource-intensive tasks. By following these simple steps, you can ensure that WSL2 uses the optimal amount of system resources for your needs.

Happy coding!

--

--