microsoft / microsoft/WSL

Better page cache reclaim mechanism

Open
#10,011 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C++
Stars
33.7k
Forks
1.8k
Avg merge
3d 17h
Merged PRs (30d)
116

Description

**Is your feature request related to a problem? Please describe.**
Currently inactive page caches in WSL2 are reported as used memory in Windows. It causes unexpected behaviors that leads to Windows application running out of memory, while part of the memory are occupied by evitable page caches in WSL2. The impact of this issue is significant especially for users (e.g. #8725 ) with small ram (<16GB) and have allocated most of their ram to WSL2.

The two ["official ways"](https://devblogs.microsoft.com/commandline/memory-reclaim-in-the-windows-subsystem-for-linux-2/) of dropping caches are not practical, since more and more applications are tightly integrated with WSL2 and inconvenient to close all of them just for dropping caches. Manually dropping cache is also not practical when user realizes the issue, as system is already lagging under low memory situation.

**Describe the solution you'd like**
A better mechanism for releasing inactive page caches from WSL2 back to host when host is running low on free memory.

**Describe alternatives you've considered**
There are several approaches to this issue that needs further discussion:
1. Host (Windows) memory management initiates page cache reclaim in WSL2 on low free memory
2. Let WSL2 awares of host memory usage and reclaim accordingly
3. Aggregate memory stats from both WSL and Windows

**Mitigation**
I wrote a service for checking host free memory via powershell every second and actively purge all page caches when host memory is running low (<1024MB). It is not a good solution as it purges all inactive caches, but it should temporary solve the issue until a better solution comes up.
```
[Unit]
Description=Pagecache Reclaim on low host free memory
RequiresMountsFor=/mnt/c/

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/bin/bash -c 'dropped=false; /mnt/c/Windows/System32/WindowsPowerShell/v1.0/Powershell.exe -Command "Get-Counter -Counter \Memory\Available\` MBytes -Continuous | Foreach-Object {\$_.CounterSamples[0].CookedValue}" | while read line; do if [ "`echo $line | tr -d '\r\n'`" -lt 1024]; then if [ "$dropped" = false ]; then echo 1 > /proc/sys/vm/drop_caches; echo "Cache Dropped"; dropped=true; fi; else dropped=false; fi; done;'

[Install]
WantedBy=multi-user.target
```
**How to use this service**
1. Enable systemd
2. Create service file /etc/systemd/system/mem-mon.service and paste the service above into this file.
3. Run command "sudo systemctl enable mem-mon; sudo service mem-mon start"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files, tests, or entry points are identified. Start by comparing the three proposed reclaim approaches with the existing WSL2 and Windows memory behavior described in the issue. Done would require an agreed mechanism, a defined low-memory trigger, and validation that inactive page caches are reclaimed without disrupting WSL2 workloads.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash, cpp, linux, powershell
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.