分步解决方法
-
定期执行缓存删除
在WSL bash上,执行
$ sudo crontab -e -u root
并添加以下行:
*/15 * * * * sync; echo 3 > /proc/sys/vm/drop_caches; touch /root/drop_caches_last_run
“ * / 15 ”表示将每15分钟执行一次。您可以根据需要更改它
-
自动启动cron服务
在〜/ .bashrc 上添加以下行:
[ -z "$(ps -ef | grep cron | grep -v grep)" ] && sudo /etc/init.d/cron start &> /dev/null
-
允许启动cron服务而无需输入root密码,
在WSL bash上执行$ sudo visudo
并添加以下行:
%sudo ALL=NOPASSWD: /etc/init.d/cron start
-
(可选) 硬限制最大内存
默认情况下,它限制为主机内存的80%,如果要更改它,请在主机上创建一个具有以下内容的.wslconfig
文件%UserProfile%
:
[wsl2]
memory=4GB
swap=8GB
localhostForwarding=true
-
最后,要确保所有更改都生效,请 wsl --shutdown
在cmd.exe上执行。重新打开您的WSL终端,然后玩得开心:)
您可以通过查看/ root / drop_caches_last_run上次修改日期来检查cron作业是否正在相应地运行:
$ sudo stat -c '%y' /root/drop_caches_last_run
https://github.com/microsoft/WSL/issues/4166