Bootstrap

Docker Desktop Vmmem内存占用过高问题解决方案

Docker Desktop Vmmem内存占用过高问题解决方案

image-20230330100900529

内存占用过高原因

主要原因是docker desktop的实现及基于wsl(Windows子系统),相当于在Windows上同时开了一个虚拟机,如果不对wsl的资源进行限制,它将会极大的获取系统资源.所以我们只需要对wsl的最大资源进行限制即可

解决方案

  1. 修改wsl配置文件

    官方地址

    具体配置:

    # 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
    
    # Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
    kernel=C:\\temp\\myCustomKernel
    
    # 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
    
    # Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
    swapfile=C:\\temp\\wsl-swap.vhdx
    
    # 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
    
  2. 在用户目录创建个.wslconfig文件,即C:\Users\<UserName>\.wslconfig

  3. 创建完成后将配置文件内容粘贴进去

    [wsl2]
    #配置wsl的核心数
    processors=2
    #配置wsl的内存最大值
    memory=512MB
    #配置交换内存大小,默认是电脑内存的1/4
    swap=8GB
    #关闭默认连接以将 WSL 2 本地主机绑定到 Windows 本地主机
    localhostForwarding=true
    #设置临时文件位置, 默认 %USERPROFILE%\AppData\Local\Temp\swap.vhdx
    swapfile=D:\\temp\\wsl-swap.vhdx
    
  4. 保存后以管理员打开powershell,执行如下语句关闭wsl:

    wsl --shutdown
    
  5. 重启docker desktop即可

后续问题(缺点)

如果内存设置的太小了,后续可能会出现docker desktop 运行一段时间退出,或者容器会突然停止工作

所以我们的memory=512MB要根据自己启动的容器所占内存大小稍大一点即可,但是如果是后续需要增加容器需要自己手动再次扩大memory

;