With release of Windows 10 Build 18917, Microsoft introduced WSL 2 to Insiders, the Windows Subsystem for Linux 2. It ships a real Linux kernel with Windows that will make full system call compatibility possible. This is the first time a Linux kernel is shipped with Windows. Windows 10 build 19013 adds another great WSL 2 feature - Memory Reclaim, which allows the host system to re-use memory that is no longer needed by any Linux process.
Previously, when the memory needs of the WSL2 Virtual Machine (VM) would grow, either from your workflow or by the Linux kernel, the overall memory allocated to the VM would also grow by allocating more memory from the host. But, once the workflow is done, that memory which is no longer needed by the workflow would not get released back to the host. Now with memory reclamation in WSL 2, when the memory in Linux is no longer needed it can be reported back to the host where it will be freed and your WSL 2 VM will shrink in memory size.
Check out the following video:
How it works
This feature is powered by a Linux kernel patch that allows small contiguous blocks of memory to be returned to the host machine when they are no longer needed in the Linux guest. The WSL team has updated the Linux kernel in WSL2 to include this patch, and modified Hyper-V to support this page reporting feature. In order to return as much memory to the host as possible, WSL periodically compacts memory to ensure free memory is available in contiguous blocks. This only runs when your CPU is idle. You can see when this happens by looking for the ‘WSL2: Performing memory compaction’ message inside of the output of the
dmesg command. If you’re a power user you can configure this behavior by editing values in .wslconfig. Please check the WSL 19013 release notes to see these options. Alternatively if you’d like to run this Linux command manually you can run the command
echo 1 > /proc/sys/vm/compact_memory as the root user.
You can find more technical details and examples in the official blog post.