How to limit Memory usage on Docker Desktop WSL 2 mode
Windows Docker Desktop container high memory usage
If you using Docker Desktop on Windows, so you know what is WSL. The WSL stands for Windows Subsystem for Linux (WSL) and it helps run a GNU/Linux environment on Windows.
Usually, you will get the below error screen when installing Docker Desktop the first time on Windows.

We complete our WSL2 setup by the steps mentioned on https://aka.ms/wsl2kernel
In this blog, we going to discuss the Docker RAM usage issue and solutions for limiting RAM usage.
System slowness
After installing Docker Desktop, I installed the Laravel Admin Panel application. When I run the application I face a slowness issue on my Laptop. So I decided to check the memory usage.
How to check Memory usage
So I opened a task manager. The VmmemWSL memory usage is around 7GB.

You can also check the RAM usage at bottom of the Docker Desktop.

Also, you can check the memory and swap space allocated to WSL usage by using the below command on the WSL terminal.
free -h --giga

How to limit Memory usage
The Docker Desktop resource configuration is available under Setting->Resources->Advanced menu. You can control the RAM and CPU usage.
Hyper-V mode

WSL mode
But we using WSL mode. So we will get the below message on the Resources Advance tab.
You are using the WSL 2 backend, so resource limits are managed by Windows. You can configure limits on the memory, CPU, and swap size allocated to WSL 2 in a .wslconfig file.

So we going to create a .wslconfig file and limit our memory usage.
1. Create a .wslconfig file
Create .wslconfig manually on the below path then add our settings.
C:\Users\
You can also create files by using the Windows command on Powershell. It will create a .wslconfig file and open it on the notepad.
notepad "$env:USERPROFILE/.wslconfig"Add the memory and processor limit. All the available configurations are listed in the Example .wslconfig file on Windows documentation.
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=22. Restart the Docker Desktop
Right-click the Docker Desktop icon and click Restart. After that start your application.

3. Verify memory limit
Now time to check the memory usage. Check the memory usage on the task manager or Docker Desktop bottom or run the free -h — giga command on the WSL terminal.

On the Docker Desktop bottom, you are able to see the RAM usage.


Now the Docker memory usage is under 4GB.
How to Monitor Docker Container Memory and CPU Usage
Docker Stats command
The docker build-in docker stats command is used to view the amount of resources your containers are using. It streams usage data for running containers.
docker stats

This docker stats command only lists the live data. So it is difficult to overall and summary of analyze the memory and CPU usage.
The docker released the Resource Usage extension, so you view your usage and the ability to analyze the data for better insights.
Docker Resource Usage extension
The Resource Usage extension is used to Monitor Container Memory and CPU Usage in Docker Desktop.
To install the extension click this link or type “Resource usage” on Add extension page.

After installing, you see the “Resource usage” menu on the sidebar. Click and view your container Memory and CPU Usage.
Chart view will give a graphical view of CPU and memory usage.


Conclusion
We limited the memory usage of Docker Desktop on WSL mode. Earlier Docker used half of my RAM, now I updated the config to use 25% of my RAM (My system's total RAM size is 16GB).
The system is running fast while using Docker. But if you face slowness on the application means increasing the RAM size on .wslconfig.
Thank you for reading.
Stay tuned for more!
Follow me at balajidharma.medium.com.






