10

It looks to me like the WSL2 container docker-desktop is using 12 GB RAM to run one container with 24 MB RAM. What gives? (details below).

Also, when I add up all the resident RAM (VSZ) from all the processes in docker-desktop, the total is only 4687251 KB or 4.4 GB, so where did the remaining 7 GB RAM go?

I tried restarting Docker Desktop from the taskbar and I tried terminating the docker-desktop WSL2 container (with wsl.exec -t), both of which made no difference. I rebooted the entire Windows machine and now it uses a much more reasonable 2GB RAM. Which is still kind of a lot compared to my Ubuntu 20.04 WSL2 that uses 595MB RAM.

So: Why is docker-desktop using 12GB RAM to serve a tiny container? Can I fix that without rebooting the entire machine?

# What is docker-desktop-data? It doesn't support any commands...
$ wsl.exe -d docker-desktop-data free -m

# docker-desktop is using approx 12 GB RAM
$ wsl.exe -d docker-desktop free -m

             total       used       free     shared    buffers     cached
Mem:         25510      12245      13264        399       1461       8191
-/+ buffers/cache:       2592      22917
Swap:         7168          2       7165

# but docker containers are only using 24 MB?
$ docker stats --no-stream

CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O         BLOCK I/O   PIDS
2f29ebe21db3   gitlab-runner   0.04%     24.55MiB / 24.91GiB   0.10%     7.49MB / 18MB   0B / 0B     30

# Sort by and sum up the VSZ values from ps in the dockerdesktop container
$ wsl.exe -d docker-desktop ps -o pid,user,vsz,rss,comm,args | perl -ane '$mem = $F[2]; $mem = $1 * 1024 if $mem =~ /(.*)m$/; printf "%9s $_", $mem; $totmem += $mem ; END { printf "Totmem: %10d\n", $totmem }' | sort | tail -n 10

   113664   259 root     111m  14m containerd-shim  /usr/bin/containerd-shim-runc-v2 -namespace services.linuxkit -id docker -address /run/containerd/containerd.sock
   113664   418 root     111m  13m containerd-shim  /usr/bin/containerd-shim-runc-v2 -namespace services.linuxkit -id socks -address /run/containerd/containerd.sock
   113664   493 root     111m  13m containerd-shim  /usr/bin/containerd-shim-runc-v2 -namespace services.linuxkit -id vpnkit-forwarder -address /run/containerd/containerd.sock
   145408  1119 root     142m  36m gitlab-runner    gitlab-runner run --user=gitlab-runner --working-directory=/home/gitlab-runner
   150528   176 root     147m  41m containerd       /usr/bin/containerd
   712704    26 root     696m  20m vpnkit-bridge    /usr/bin/vpnkit-bridge --pid-file=/run/vpnkit-bridge.pid --addr=stdio guest
   723968    16 root     707m  25m wsl-bootstrap    wsl-bootstrap run --base-image /mnt/host/c/Program Files/Docker/Docker/resources/wsl/docker-for-wsl.iso --version-pack /mnt/host/c/Program Files/Docker/Docker/resources/docker.tar --cli-iso /mnt/host/c/Program Files/Docker/Docker/resources/wsl/docker-wsl-cli.iso
   729088   234 root     712m  30m diagnosticsd     /usr/local/bin/diagnosticsd
   730112   316 root     713m  31m lifecycle-serve  /usr/bin/lifecycle-server
Totmem:    4687251

1 Answer 1

8

Take a look at WSL2 configuration file documented here:
https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig
You can limit usage of WSL engine to let say 3GB and 2vCPU by creating %userprofile%\.wslconfig file as follow:

[wsl2]
memory=3GB
processors=2

As I understand the memory is shared between all WSL namespaces/distros and what are you seeing typing free is memory occupied mostly by file cache in your other distros. To quickly and in a dirty way free up your RAM you execute as root sync; echo 3 > /proc/sys/vm/drop_caches in any of your WSL2 distros.
According to docker-desktop-data HERE you can check answer, simply it is backing storage for your containers

Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic! You're right... It is sneaky though. The total of (used+buffers) seems to be similar in all the WSL distributions (how could they call them "distributions"?), but what in one was a little used RAM and lots of buffers in another was shown as lots of used RAM. Go figure. But yes, this was the piece I was missing: The memory is shared. (I don't want to limit it, though...)
Yeah def seems like a docker-desktop bug if they're reporting full WSL2 ram usage vs. just what docker is using as it claims.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.