Table of Contents
In some cases, you want to configure or limit the memory (RAM) for a WSL instance. By default, WSL will take 50% of the host memory. You can check how much memory and swap space are allocated to WSL using the free command from within a WSL distribution:
sudo free -h --giga
From the Task Manger, you can check the total of RAM that WSL is consumed by finding a process named VmmemWSL.
Whatever the case may be, if your machine doesn’t have enough memory to begin with, then you’ve probably seen some noticeable slowdowns due to WSL 2. Your machine may become sluggish as it struggles to run resource-hungry host processes.
How to Configure Memory Limits in WSL2
You can tell WSL 2 how much RAM, swap space, CPU cores, and other resources it should allocate by creating a special .wslconfig file under your Windows user profile directory (C:\Users\YourUsername\.wslconfig). On launch, WSL will read this file if it exists and configure itself accordingly.
1. First, right click on the Windows Start icon then open an elevated PowerShell window.
2. Run the following command to edit the WSL global configuration file using the Notepad.
notepad $home\.wslconfig
3. This is the first time you edit the file. So, it will ask you to create the file. Click Yes to create the file.
4. Enter the following line then save the configuration file. We will limit the memory of WSL to 1GB. You can change it to any value as you need.
[WSL2]
memory=1GB
5. You can either close out of WSL manually and wait a few seconds for it to fully shut down, or you could launch Command Prompt or PowerShell and run the following command to forcibly shut down all WSL distributions:
wsl --shutdown
Relaunch your WSL distribution when the above command finishes executing.
wsl -d ubuntu
6. Finally, run the free command again to verify that WSL respects your specified resource limits. As you can see, the memory of the WSL instance is limited to 1GB.
Not a reader? Watch this related video tutorial: