Understanding your WSL2 RAM and swap - Changing the default 50%-25%
The Windows Subsystem for Linux operates as a virtual machine that can dynamically grow the amount of RAM to a maximum set at startup time. Microsoft sets a default maximum RAM available to 50% of the physical memory and a swap-space that is 1/4 of the maximum WSL RAM. You can scale those numbers up or down to allocate more or less RAM to the Linux instance.
The first drawing shows the default WSL memory and swap space sizing. The images below show a developer machine that is running a dev environment in WSL2 and Docker Desktop. Docker Desktop has two of its own WSL modules that need to be accounted for. You can see that the memory would actually be oversubscribed, 3 x 50% if every VM used its maximum memory. The actual amount of memory used is significantly smaller allowing every piece to fit.
Click to Enlarge |
Linux memory under WSL2
Idle 64GB Windows with WSL Default: idle
64GB Windows with WSL 48GB/16GB: idle
64GB Windows with WSL 48GB/16GB: running 60GB Python
My machine didn't enough RAM to give up to the Linux VM. Instead, I allocated 48GB of RAM and 16GB of SWAP. 48GB+16GB was 64GB of program space, enough to run the Python program.
.wslconfig
- Linux prompt
- Run "free -m" to see current configuration
- Windows prompt
- See what WSL is running "wsl --list --verbose"
- Shutdown WSL with "wsl --shutdown"
- Terminal connections will terminate
- Docker and VSCode (remote) will complain and ask if you want to reconnect
- Create .wslconfig file in your Windows home directory
- Launch WSL linux by opening a terminal prompt.
- Tell Docker it can restart WSL integration or restart docker
- Tell VSCode (remote) it can reconnect.
- Linux Prompt
- Run "free -m" to verify the changes were applied
custom memory and swap: 48GB/16GB
Video
Created 2022 01
Comments
Post a Comment