55

I recently updated my Docker environment to run on WSL 2 on Windows.

For setting memory allocation limits on containers in previous versions, I had option in Docker Desktop GUI under Settings->Resources->Advanced->Preferences to adjust memory and CPU allocation.

After WSL 2 integration, I am not able to find that option. enter image description here

I assume I should run everything through my Linux distro from now on, so this is the solution I was able to find:

docker run -d -p 8081:80 --memory="256m" container_name

I dont want to have to set a flag each time when running a container. Is there a way to permanently set the memory allocation?

2
  • Did this command work for a specific container docker run -d -p 8081:80 --memory="256m" container_name?
    – srk
    Oct 6, 2020 at 5:35
  • Yes it did @srk Oct 7, 2020 at 16:58

2 Answers 2

93

The Memory and CPU settings were removed for WSL2 integration. However, starting in Windows Build 18945, there is a workaround to limit WSL2 memory usage.

Create a %UserProfile%\.wslconfig file for configuring WSL2 settings:

[wsl2]
memory=6GB  # Any size you feel like (must be an integer!)
swap=0
localhostForwarding=true

Run Get-Service LxssManager | Restart-Service in an admin Powershell (or reboot) and verify that the vmmem usage in Task Manager drops off.

For the complete list of settings, please visit Advanced settings configuration in WSL.

14
  • 6
    Yes, I did this. Infact I pushed the Microsoft documentation team to improve docs regarding the .wslconfig file. There is an ongoing issue where Vmmem takes up large chunk of memory, they plan to resolve it soon. This solution only addresses the memory allocated to Linux subsystem. I was looking for more granular control over each containers resources. Thanks for the answer though. Ill wait for a few more days for appropriate answer. Jul 7, 2020 at 21:56
  • 5
    Restart is not necessary, just run "Get-Service LxssManager | Restart-Service" in your admin powershell
    – Ilendir
    Mar 4, 2021 at 13:24
  • 2
    @srk the answer is indeed relevant to Docker for those who are running Docker Desktop for Windows using the WSL2 back end, because then the Docker daemon is running in WSL.
    – Stabledog
    Jul 22, 2021 at 11:32
  • 12
    The ".wslconfig" file does not support floating-point numbers. If you set memory=2.5GB, it just ignores your settings, without any warning. Use integers. Jul 29, 2021 at 22:36
  • 2
    @TrojanName make sure your WSL 2 VM has enough memory allocated. I allocate 4GB in mine. Also, if it's your container not having enough memory, it can be your Docker compose configuration (in case you have one). Make sure you're not setting a resource limit. It's under services.<service-identifier>.deploy.resources.limits.memory. Details here: docs.docker.com/compose/compose-file/compose-file-v3/#resources
    – Alexz S.
    Feb 28 at 15:11
12

You must limit WSL memory usage...

Step 1

Add/Edit this file %UserProfile%\.wslconfig and append these two lines:

[wsl2]
memory=8GB

Step 2

Do a full shutdown right after for WSL to pick up the new settings:

$ wsl --shutdown

See additional information from Microsoft here: Advanced settings configuration in WSL


Experimental Solution (September 2023 Update)

This September 2023 update added support for new opt-in experimental features, including the new autoMemoryReclaim which makes the WSL VM shrink in memory as you use it by reclaiming cached memory. If you want to try it...

Add this new experimental section to your %UserProfile%\.wslconfig file:

[experimental] 
autoMemoryReclaim=gradual

See this and more experimental settings here: Windows Subsystem for Linux September 2023 update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.