我用的电脑一共有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,此后占用内存就很少了。

在这里插入图片描述

GitHub 加速计划 / ws / WSL
21
0
下载
Issues found on WSL
最近提交(Master分支:6 个月前 )
8c61f09e * Add super user detection; a powershell approach is used * use the 'id' command * avoid stderr warning & wsl alias 1 个月前
ae780d11 1 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐