24

Windows Subsystem For Linux 2 的内存占用问题

 3 years ago
source link: https://blog.251.sh/wsl2-consumes-massive-amounts-of-ram-so-how-to-solve-it-_
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Windows Subsystem For Linux 2 的内存占用问题

February 7, 2021

注:这篇文章关注 WSL2,并非 WSL1。

电脑就开了几个浏览器窗口 + wsl2,内存占用就要爆了——资源管理器一看,是一个叫做 Vmmem 的进程。

在 WSL2 的 repo 里一照,最高票的 issue 也是这个问题。那么有什么办法可以解决呢?

方法一:及时清除占用

如果只是偶尔发生,那么可以通过运行这行命令解决:

echo "sync && echo 3 > /proc/sys/vm/drop_caches" | sudo sh

方法二:限制总内存占用

在你的用户文件夹下(即 %UserProfile%)创建一个叫做 .wslconfig 的文件,并写入以下内容:

[wsl2]
memory=8GB
swap=4GB
localhostForwarding=true

memory 和 swap 的大小可以自己调整。这个方法一个潜在的问题是,如果某个程序持续的占用内存但内存却不够了,可能会被 Kill 掉。

方法三:cron + 方法一

在 root 的 crontab 内输入:

* * * * * echo 3 > /proc/sys/vm/drop_caches; touch /root/drop_caches_last_run

这样的话,就是每分钟清除一次占用。不过在设置之后,需要在 .bashrc 内添加 [ -z "$(ps -ef | grep cron | grep -v grep)" ] && sudo /etc/init.d/cron start &> /dev/null,并且执行 visudo 添加 %sudo ALL=NOPASSWD: /etc/init.d/cron start

通过这个命令,你可以测试是否在自动运行:

sudo stat -c '%y' /root/drop_caches_last_run

ArchLinux on WSL2 不支持这个方法。我正在想一个用 genie 支持的方法,但是 ArchLinux 官方的 dotnet sdk 还没有更新。 Update (2021-04-17): 通过给 ArchLinux 弄一个 systemd 的方法(方法二),在启用 cronie 之后就能正常执行 root 的 cron 任务了。

但终究,看起来修复还需要等待好一段时间

如果想试一试效果,目前可以参考这里,使用的是这个命令:

dd if=/dev/zero of=./dump.bin bs=4k iflag=fullblock,count_bytes count=20G

注:我不对您的电脑负责,请先了解一下是什么再执行。

顺便,WSL2 还有其它小问题……例如不会自动释放空间,不过这应该是 Hyper-V 的毛病吧。


如果喜欢本文,欢迎点击下方的「鼓掌」按钮!

如果上面没有加载出任何东西,可以点击这里


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK