WSL 配置 Docker 内存和 CPU 资源限制

我用的电脑一共有40G内存,最近发现电脑重启后,VmmemWSL 进程很快就会占用一多半的内存(20+G),电脑中有多个停止运行的容器,正常启动状态的只有一个 MySQL 服务,通过 docker stats 查看占用内存也不多,不知道为什么会占用这么多内存,但是必须限制一下。

Docker Desktop 早期版本可以直接配置内存使用,改成 WSL 方式后需要去调整 WSL 的资源使用,参考官方文档中的示例 做了以下调整:

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 8 GB, this can be set as whole numbers using GB or MB
memory=8GB 

# Sets the VM to use two virtual processors
processors=4

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false

# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true

# Enable experimental features
[experimental]
autoMemoryReclaim=gradual
sparseVhd=true

将上面内容保存到 C:\Users\用户名\.wslconfig 文件中。

上面配置限制内存和交换内存都是 8G,可以根据自己内存进行调整。配置autoMemoryReclaim=gradual后还可以 检测空闲 CPU 使用率后,自动释放缓存的内存。 设置为 gradual 以慢速释放,设置为 dropcache 以立即释放缓存的内存。

修改配置后关闭 Docker,关闭 WSL(wsl --shutdown),然后启动 WSL 和 Docker,此后占用内存就很少了。

在这里插入图片描述

相关推荐

  1. 配置etcd、apiserver使用的cpu内存资源

    2023-12-11 00:10:03       131 阅读
  2. docker资源限制

    2023-12-11 00:10:03       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 00:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 00:10:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 00:10:03       82 阅读
  4. Python语言-面向对象

    2023-12-11 00:10:03       91 阅读

热门阅读

  1. Docker入门:容器化原理

    2023-12-11 00:10:03       71 阅读
  2. 08 Python文件待更新

    2023-12-11 00:10:03       59 阅读
  3. html,css,开发知识,调试知识

    2023-12-11 00:10:03       52 阅读
  4. Class 与 Style 绑定

    2023-12-11 00:10:03       59 阅读
  5. Torch2TRT编译和使用踩坑

    2023-12-11 00:10:03       71 阅读
  6. Vue3源码梳理:源码目录结构及源码阅读方法

    2023-12-11 00:10:03       60 阅读
  7. 全网最全的正则匹配规则,整理多家网站案例

    2023-12-11 00:10:03       47 阅读
  8. 网卡学习资料整理

    2023-12-11 00:10:03       68 阅读