microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.3k stars 814 forks source link

Option to reclaim memory even when there are background processes (disowned or nohup) #7470

Open tigerinus opened 3 years ago

tigerinus commented 3 years ago

Is your feature request related to a problem? Please describe.

I use powerline to customize the shell prompt (it has TMUX integration where others don't).

The tool always have a powerline-daemon process running at the background.

This prevents WSL2 from reclaiming the memory even when I exit all the shell prompts.

> wsl -- ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0    900   528 ?        Sl   20:28   0:00 /init
root         8  0.0  0.0    900    84 ?        S    20:28   0:00 /init
tigerin+  1277  3.7  0.4 141228 53112 ?        Sl   20:42   0:00  \_ /usr/bin/python3 /home/tigerinus/.local/bin/powerline-daemon -q
root      1571  0.0  0.0    900    84 ?        Ss   20:42   0:00 /init
root      1572  0.0  0.0    900    84 ?        S    20:42   0:00  \_ /init
tigerin+  1573  0.0  0.0   9668  3300 pts/0    Rs+  20:42   0:00      \_ ps auxf

Describe the solution you'd like

It'd be nice to add an option, such that when there is no active pts session, i.e. shell session, regardless how many headless (i.e. pty-less) processes, WSL2 will proceed and reclaim the memory.

Describe alternatives you've considered

I've tried including a script in .bash_logout such that when this is the last PTY session, call powerline-daemon -k to kill the background process, so later WSL2 will be able to reclaim.

Obviously, this works for this specific process only.

OneBlue commented 3 years ago

This is an interesting problem. From WSL's perspective there's no way for us to know that this process is not 'important' and so the distro will keep running until it exits.

On this I'd say that it would be easiest to try to solve this from the powerline-daemon side and maybe have it timeout after a while if no shell is connected to it but looking at its documentation I don't see any easy way of doing that right now

tigerinus commented 3 years ago

I have following logic in ~/.bash_logout (in case people came here for the same issue):

if [[ $(ls -l  /dev/pts | grep $USER | wc -l) = 1 ]]; then
    powerline-daemon -k
fi

I agree with you that there is no way to know which process is important or not. That's why I propose this feature as an option for people to opt-in.

For me, WSL2 is more of a command-line experience for coding, rather than a Linux environment for running complicated background processes. Killing everything after I exited the last prompt is perfectly fine. This option would serve users like me.