avatarBalaji Dharma

Summary

This article provides a solution for limiting memory usage on Docker Desktop in WSL 2 mode to prevent system slowness.

Abstract

The article begins by explaining that users of Docker Desktop on Windows may experience high memory usage and system slowness, especially when running applications like Laravel Admin Panel. The author shares their experience with this issue and provides steps to check memory usage using Task Manager, Docker Desktop, and the WSL terminal. The solution involves creating a .wslconfig file to limit memory and processor usage. The article also discusses the Docker Stats command and the Docker Resource Usage extension for monitoring container memory and CPU usage. The author concludes by sharing their personal experience with limiting memory usage on Docker Desktop and recommends increasing the RAM size in .wslconfig if application slowness persists.

Bullet points

  • High memory usage and system slowness can occur when using Docker Desktop on Windows, especially with applications like Laravel Admin Panel.
  • To check memory usage, open Task Manager, check RAM usage at the bottom of Docker Desktop, or use the free -h --giga command on the WSL terminal.
  • The Docker Desktop resource configuration can be accessed under Settings > Resources > Advanced menu to control RAM and CPU usage.
  • In WSL mode, memory and processor limits are managed by Windows, and can be configured in a .wslconfig file.
  • Create a .wslconfig file manually or use the notepad "$env:USERPROFILE/.wslconfig" command on Powershell to create and open it.
  • Add memory and processor limit settings in the .wslconfig file, using the example provided in the article.
  • Restart Docker Desktop after updating the .wslconfig file and verify memory limit using Task Manager or the free -h --giga command on the WSL terminal.
  • The Docker Stats command and the Docker Resource Usage extension can be used to monitor container memory and CPU usage.
  • The article concludes with a personal experience of limiting memory usage on Docker Desktop and a recommendation to increase the RAM size in .wslconfig if application slowness persists.

How to limit Memory usage on Docker Desktop WSL 2 mode

Windows Docker Desktop container high memory usage

Photo by Ian Taylor on Unsplash

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.

VmmemWSL memory usage

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\\.wslconfig

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=2

2. 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.

Docker
DevOps
Open Source
Laravel
Web Development
Recommended from ReadMedium