Docker 和 wsl 占用大量内存 VmmemWSL,有什么好的解决办法么?

动机

最新会员文章谈及 Docker (2024 年,如何为 Windows 的 Linux 子系统(WSL 2)解决网络问题? - #UNTAG (utgd.net) )碰巧,我也遇到一个相关问题特此提问。

问题描述

我的 Windows 安装了 WSL Ubuntu 22.04.3 和 Docker Desktop. 但是它们平时占用大量的内存。(见任务管理器中的 VmmemWSL)

  • 我在 Ubuntu 上做科学计算,因此内存不能过小。

大家也遇到过这种情况么?怎么解决的?

我的尝试

我的两台电脑上都遇到了这个问题。上面截图中的电脑A有 32 G 内存。另一台电脑B 有 16G 内存。

在电脑B 上,我卸载了 Docker Desktop, 并将 Docker 安装到 wsl Ubuntu 22.04.3 上了。

  • 优势:减少 wsl 子系统的数量
  • 劣势:安装在 WSL 中的 Docker 无法有效调用显卡。(参考下面 docker-compose 中截取的“调用显卡” 的代码)
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

我找到个帖子跟你说的情况很类似,可以参考下:

帖子里的解决方法是配置 WSL 的配置文件,限制内存上限。考虑到你的需求,你可以适当提高一些内存上限。保留电脑正常运行所需要的内存大小。

所以看起来是个 WSL 问题,跟 Docker 关系不大(虽然Docker 跑起来也确实很占内存就是了)。

如果你要用显卡的话,还是建议在 Windows 上安装 Docker Desktop,个人感觉能省很多事。

还有一个思路是在 Docker Desktop 里限制容器内存,或者试试新加的休眠功能,应该可以在你不用的时候减少内存占用。

终极解决办法应该是加内存 :joy:

感谢回复。

WSL 好像只允许全局配置,而非针对每个子系统设定内存。因此,无法实现

  • Ubuntu 子系统最多 8G内存
  • Docker 子系统最多 2G内存

WSL 中的 Ubuntu 和 Docker 大概都有内存释放问题。在我的电脑上,启动这些子系统后,输入 wsl -l -v 得到

  NAME                   STATE           VERSION
* Ubuntu-22.04           Running         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

不明白为什么 Docker 要启动两个子系统。

其中每个子系统开机既占用大概 1G内存。如果随便运行几个 Docker 容器,内存很快就会被占用。且,删除容器后,内存无法快速释放。

通常,为了释放内存,我只知道重启 Docker Desktop 这一个方法。

因此,我想:与其「多个 WSL 分别都占着内存不用」不如「将 Docker 安装在 WSL Ubuntu 下」。我在B电脑上实践的正是这个思路。

这样做的局限性在 原始贴中已经提过了

  • 优势:减少 wsl 子系统的数量
  • 劣势:安装在 WSL 中的 Docker 无法有效调用显卡。

未来方案

思路0:加大内存

如你所建议的那样,「加大内存」是最直接的思路。但看到 (WSL 2 consumes massive amounts of RAM and doesn't return it · Issue #4166 · microsoft/WSL · GitHub

I’m running an instance of PhotoPrism in WSL via Docker. When indexing a photo library of about 10,000 photos my memory eventually fills to 100% saturation followed by WSL crashing. We’re talking 128GB of available memory here as well…

所以,「加内存」难说是不是一个妥善的办法。

思路1:配置一台机器,专门运行 Docker

我正在考虑在 Ubuntu 24.04.1 发行之后,将 B 电脑的系统替换为 Ubuntu Desktop 24.04 这个版本。原因如下

  • 需要 Windows 系统时,有 A电脑供使用;
  • Ubuntu 24.04 的 Linux 内核为 6.x 支持 12代 Intel CPU 的大小核设计(A、B电脑都是 12th gen Intel CPU);
  • Ubuntu Desktop 版本安装时(和安装后)有图形界面。考虑到校园网需要从浏览器网页端认证(除非申请固定IP),因此,总体上,安装 Ubuntu 比(同为 Linux Kernel 6.x 的) Debian 12 简单。

思路2:期待 WSL 升级修复这个问题

如小节标题《期待 WSL 升级修复这个问题》。

但是悲观的是, WSL 2 consumes massive amounts of RAM and doesn’t return it · Issue #4166 · microsoft/WSL (github.com) 这个 issue 提于 2019 年,至今开放。

所以,「待微软解决?」尚遥遥无期。

看到这个我明白了,我本来的意思是在 WSL → Ubuntu-22.04 的内部安装和运行 Docker ,这样 Docker 可用的资源就会受 WSL 控制。

所以我猜你应该是用的 WSL 版本的 Docker: Docker Desktop WSL 2 backend on Windows | Docker Docs

这个文档里提到了一点——

For a better experience on WSL, consider enabling the WSL autoMemoryReclaim setting available since WSL 1.3.10 (experimental).

This feature causes the Windows host to better reclaim unused memory inside the WSL virtual machine, thereby resulting in better memory availability to other host applications. This is particularly helpful with Docker Desktop, since otherwise the WSL VM may consume large amounts (GBs) of memory in the Linux kernel’s page cache as Docker builds container images, without ever returning that memory to the host when it becomes unused inside the VM.

感觉你可以尝试下在 .wslconfig 文件里加上如下配置:

[experimental]
autoMemoryReclaim=dropcache

可用参数:

  • gradual 缓慢释放
  • dropcache 立即释放

如果实在不行,感觉这个思路可行性比较高,而且能解决显卡问题。

1 个赞
  • 电脑A (32G 内存)的情况是 Ubuntu-22.04 与 Docker Desktop 并列。都是 WSL 2 backend.
  • 电脑B (16G 内存)的情况是 Ubuntu-22.04 内部又安装了 Docker.

我常用电脑B。我可以试试