Microsoft has released the latest Windows Subsystem for Linux update, adding Automatic Memory Reclaim.
Windows Subsystem for Linux (WSL) allows developers to run a Linux instance with Windows, giving them access to Linux software. WSL is a popular option for developers creating and testing software for Linux.
In the September 2023 update, Microsoft has added Automatic Memory Reclaim to the list of features:
When this is set to gradual, after being idle for 5 minutes, WSL will slowly start to release cached memory in Linux and make it available as free memory back to the Windows host. This means that your WSL VM will automatically shrink in memory size when you’re not using it!
This works by WSL detecting that you’re idle by seeing if CPU usage is continuously low for 5 minutes, and then we start reclaiming cached memory by using the cgroup memory.reclaim feature. We reclaim a fixed portion of your VM’s memory size, which is calculated so that if your VM was full of cached memory it would go to zero cached memory after 30 minutes (e.g: If you have 3000MB of memory, we reclaim 100MB every minute). The memory.reclaim cgroup feature allows us to intelligently reclaim a portion of memory over time, striking a balance between performance and memory usage. However, this feature does require disabling cgroups v1 in WSL, which can cause some issues. In early testing we noticed that this will break the docker daemon when running it as a service in WSL, and so if you’re using this feature we recommend you use Docker Desktop for your docker needs. We are working with the Docker team to address this in the future.
Microsoft has also improved Automatic Disk Space Clean Up:
WSL virtual hard disks (VHDs) grow in size as you use them, and now with this feature enabled they will automatically shrink in size too! This new setting automatically sets any new VHD to be a sparse VHD, which can automatically reduce their size. Additionally, we’ve added the wsl –manage–set-sparse <true/false> command to allow you to set your existing distros to be sparse or not if you’d like.