Skip to content

Commit

Permalink
Add protection against WSL 2 using all your memory
Browse files Browse the repository at this point in the history
At the time of writing this commit there's a pending issue for WSL 2
where it happily eats all of your memory over time.

Details are at: microsoft/WSL#4166

I ran into this issue naturally starting and stopping Docker containers
for half a day while doing my normal work. Before I knew it, the VM was
using 11GB of memory (the default is to use 80% of your RAM).

I'm not going to bikeshed on this by making a cronjob run the alias
command on a schedule because I imagine this will be fixed in a hotfix
at some point in the future.

Just be mindful of this if you notice that the Vmmem process is using a
ton of memory.
  • Loading branch information
nickjj committed May 29, 2020
1 parent 5674a31 commit d0f1fc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .aliases
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ alias 755d="find . -type d -exec chmod 755 {} \;"
alias 644f="find . -type f -exec chmod 644 {} \;"

alias open="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"

# This is specific to WSL 2. If the WSL 2 VM goes rogue and decides not to free
# up memory, this command will free your memory after about 20-30 seconds.
# Details: https://github.com/microsoft/WSL/issues/4166#issuecomment-628493643
alias drop_cache="sudo sh -c \"echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'\""
9 changes: 9 additions & 0 deletions c/Users/Nick/.wslconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[wsl2]
memory=6GB # Make sure you have this amount available to use!
swapFile=E:\\wsl2-swap.vhdx # I put mine off my SSD, but this is optional.

# Another option is to disable swap but if the VM goes out of memory, the
# OOM killer might start killing important processes or the VM may crash.
#
# Uncomment the line below this to use no swap file at all in the WSL 2 VM.
#swap=0

0 comments on commit d0f1fc2

Please sign in to comment.