The tool which doesn't interrupt your processes, drops the cache if and only if the machine is idle.
In WSL2 (Windows Subsystem for Linux, version 2), there's a bug. Due to that issue, WSL2 doesn't return the cache, instead, the amount of cache grows until the WSL2 instance's assigned RAM is full. Well, you can simply drop the cache. But, there would be a problem with the speed of your process. Instead, the script makes sure that the WSL2 instance is idle, then drops the cache. With that approach, we could eliminate the speed issue, for some of the cases.
- Install Python 3.6+ (with
pip
), withpsutil
installed. Make sure thatpsutil
is not installed as user; for example, don't dopip3 install psutil
. Instead, install it withsudo
or if your distro has the related package something likepython3-psutil
, go with it. - Clone the repo, you can find
drop_cache_if_idle
script there. Make sure that the script is executable. You can simply executechmod +x drop_cache_if_idle
to ensure the executability. - Copy
drop_cache_if_idle
file on one of your$PATH
, for example/usr/bin/
. - On your WSL bash execute
sudo crontab -e -u root
and add the following line:*/3 * * * * drop_cache_if_idle
. The "*/3" means that it will be executed every 3 minutes. You can change it if you wish. - On your
~/.bashrc
add the following line:[ -z "$(ps -ef | grep cron | grep -v grep)" ] && sudo /etc/init.d/cron start &> /dev/null
. - On your WSL bash execute
sudo visudo
and add the following line:%sudo ALL=NOPASSWD: /etc/init.d/cron start
. - Then, run
wsl --shutdown
on cmd.exe. This command will shut all of your instances of WSL2 down.
Thanks to the comment comment on the issue of WSL repository and repo AdnanHodzic/auto-cpufreq for providing the related information used in this repo.
Also, you can use arkane-systems/wsl-drop-cache. This is a systemd-based implementation of that package, using the same underlying Python script, but modified to run from a systemd timer and to be installed using an apt package.
Additionally, it appears that the individuals at Microsoft have implemented something similar to what this script does in WSL2/1.3.10. Therefore, you have the option to utilize that approach instead of this project.