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
The second drawing shows the memory allocation on my 64GB laptop.

WSL Linux defaults to a maximum RAM size of 50% of the host machine's physical RAM.  WSL Linux creates a swap disk and sizes the max to be 25% of the maximum RAM. 

Linux memory under WSL2 

You can see how much memory is available to a running WSL2 instance by executing the free -m command from a command prompt in the Linux instance.

Idle 64GB Windows with WSL Default: idle

An example Linux instance running under WSL on a 64GB RAM PC has access to 32GB.  This is an idle Linux instance so the actual physical memory in use is significantly less.

$ free -m
total used free shared buff/cache available Mem: 31794 121 31615 0 57 31391 Swap: 8192 0 8192

64GB Windows with WSL 48GB/16GB: idle

An example Linux instance running under WSL2 on a 64GB RAM PC with a .wslconfig file in the users home directory. The configuratrion allocates 48GB RAM and 16GB swap space for each WSL2 instance.

$ free -m
total used free shared buff/cache available Mem: 48190 96 48059 0 33 47726 Swap: 16384 0 16384

64GB Windows with WSL 48GB/16GB: running 60GB Python 

I ran a Python program that consumes 60 GB of memory. It crashed on my 64GB machine because there were only 32GB+8GB=40GB available inside the Linux instance. 

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.

$ free -m
total used free shared buff/cache available Mem: 48190 44980 2800 95 409 2518 Swap: 16384 15454 929

.wslconfig 

This sample file is put in your Windows home directory.  You need to stop and restart the Windows Subsystem for Linux for this to take effect.  The documentation says to wait 8 seconds before restarting the WSL
  1. Linux prompt
    1. Run "free -m" to see current configuration
  2. Windows prompt
    1. See what WSL is running "wsl --list --verbose" 
    2. Shutdown WSL with "wsl --shutdown"
    3. Terminal connections will terminate
    4. Docker and VSCode (remote) will complain and ask if you want to reconnect
  3. Create .wslconfig file in your Windows home directory
  4. Launch WSL linux by opening a terminal prompt.
    1. Tell Docker it can restart WSL integration or restart docker
    2. Tell VSCode (remote) it can reconnect.
  5. Linux Prompt
    1. Run "free -m" to verify the changes were applied

custom memory and swap: 48GB/16GB

# Settings apply across all Linux distros running on WSL 2
# Can see memory in wsl2 with "free -m" # Goes in windows home directory as .wslconfig [wsl2] # Limits VM memory to use no more than 48 GB, defaults to 50% of ram memory=48GB # Sets the VM to use 8 virtual processors processors=8 # Sets the amount of swap storage space to 8GB, default is 25% of available RAM swap=16GB

Video

Created 2022 01

Comments

Popular posts from this blog

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

Almost PaaS Document Parsing with Tika and AWS Elastic Beanstalk