If you've ever had the misfortunate of working with Docker on a Windows machine you are in the right place.
Also, if you've arrived here via a google search on "how to get memory back from #!$@%^ing DOCKER" here is the cheat sheet so you can be on your merry way 🎅
# documentation: https://learn.microsoft.com/en-us/windows/wsl/wsl-config#wslconfig
# actual contents of .wslconfig @ %UserProfile%
[wsl2]
processors=10
memory=2123366400
swap=1073741824
[experimental]
autoMemoryReclaim=gradual
# the WSL documentation suggests finding the GUI and configuring there:
# Start > WSL Settings
# the default is half of your RAM (reduce this further)
# I think the default is all of the processors (e.g. 20) (I reduced this to 10 or 5)
# comprehensive blog with example .wslconfig
# https://www.valens.dev/blog/Take-control-of-your-WSL-resources-for-smooth-development
Now for my thoughts...
Warning!
This blog is going to piss a lot of people off because...
- Everyone LOVES Docker (I don't)
- I curse a lot in this blog... (It's censored)
Docker is an @$#hole on Windows🙂
As a native Windows developer who works almost exclusively in the Microsoft tech stack (TypeScript, C#/.NET, MS SQL Server, Azure) I have no reason to go near Windows Subsystem for Linux... that is... until Docker enters the picture... 🙄
I have worked with Docker for two years (feels like devs don't often get a choice in the matter) and only last week did I discover the existence (the necessity...) of the .wslconfig file.
For some reason, WSL2 comes ready to steal massive amounts of your machine's processing power out of the box. Docker on Windows "requires" WSL for its backend (presumably for the container/VM orchestration). The defaults for what your VMs can partition from your hardware are as follows:
-
100%of your CPU cores -
50%of your RAM -
n%of your hard disk
This feels problematic in general but allow me the pleasure of listing explicit reasons why: (why you might want to relegate only the bare minimum processes to your WSL VMs and keep the vast majority of your processing power on the Windows partition)
- You want access to a desktop GUI and native apps*
- You want access to your primary file system
- You want to limit the amount of latency you have to deal with
- You work in enterprise which runs anti-malware and other spyware on your machine which will also hog resources...
*Yes, I know, WSLG exists and is actually goated...
TL;DR - You want to preserve as much processing power as possible on your primary partition for a litany of valid reasons.
So, why does WSL think you want to surrender ONE-HUNDRED-PERCENT OF YOUR CPU CORES AND FIFTY PERCENT OF YOUR RAM??? 🤔🤔🤔
Idk...
But anyway, here is how to fix it: Bottleneck Hardware Thresholds in the wslconfig File.
Step 1 - Create a .wslconfig File
The official Microsoft Documentation strongly suggests you do this through the Windows UI so I'll list those steps.
(see the top "cheatsheet" if you want to raw-dog it and just
cd ~(your%USERPROFILE%most likely)ni .wslconfigetc etc etc)
Legend:Blah Blah Setting: My - Choice (suggested percent hardware capacity)
- Navigate to: Start > WSL Settings
Step 2 - Bottleneck Memory and Processor
- Adjust settings in "Memory and processor"
- Processor Count: 10 - 15 (50-75%)
- Memory Size: 2000MB - 8000MB (6.25% - 25%)
- (2000MB is the minimum for SQL Server)
- Swap Size: 1024MB (0.002%+)
Step 3 - Reclaim Memory During Runtime
By default, Windows will only reclaim memory from the partitions automatically on cache drop, but with an experimental feature we can reclaim unused resources while the VMs are running.
- Adjust settings in "Optional Features"
- Auto memory reclaim: Gradual
Step 4 - Restart WSL
Either close Docker Desktop or whatever or run the following commands
# see if WSL is running anything
wsl --list --running
# if it is, teriminate it
wsl --shutdown
Congratulations! 🎉🎈You just reclaimed tons of processing power that Docker and WSL stole without your permission!🥳
Addendum
- This config will apply thresholds/bottlenecks to the entire WSL orchestrator, meaning, if you need more resources than a single SQL Server Instance and some Functions, these ratios will not work for you.
- "Swap Size" creates a file on disk which will use native I/O computational processing power for the spills when your VM needs to exceed the RAM/memory bottleneck. This can have adverse effects on the health of your physical disk. DO NOT USE SWAP SIZE/FILE IF YOU HAVE AN HDD (each HDD has a finite number of READS/WRITES in its lifetime)
#docker #wsl #vm #local #performance #bottleneck #ram #memory #cpu #disk
Top comments (0)