The Limit of Memory Usage in WSL2

date
Mar 7, 2023
slug
wsl2-limit-memory
status
Published
tags
Linux
summary
WSL2 has a default memory usage limit that is set to half of the host memory.
type
Post
When running a Go program that uses more than 12GB of memory, it stops once the memory usage reaches 12GB. The command line doesn't show any errors, panics, or output, and the program stops silently.
~ Run go test ./
=== RUN   TestDBMetaphysicsProblem
--- PASS: TestDBMetaphysicsProblem (10.94s)
=== RUN   TestDBSequentialOP
FAIL	github.com/therainisme/mlsm	182.824s
If you check the host memory usage in the task manager, you'll find that the program used up to half of your memory before stopping. To fix this, I configured the WSL2 memory limit.
To configure the memory limit, I created a .wslconfig file in my user folder (which is located at C:\users\me), and added the following content:
[wsl2]
memory=16GB
After that, I rebooted WSL2 with the command wsl --shutdown. To verify the new memory setting, I ran the free command in WSL2, which displayed information like the following:
~ free
               total        used        free      shared  buff/cache   available
Mem:        16381032      909836    14426152         980     1045044    15175252
Swap:        4194304       87336     4106968
The units are in bytes. With the memory usage limit in WSL2 successfully configured, the Go program can now run correctly.

© therainisme 2025