4

I recently tried installing Ubuntu on Windows (WSL2). I tried the free command, and few blocks of swap memory were utilized. I thought of reducing the swappiness so I added the entry vm.swappiness=10 in /etc/sysctl.conf. I tried reloading sysctl.conf, and it seemed to work, But when I tried shutting down WSL and starting a fresh session, swappiness was reset to the default value of 60.

Is Ubuntu on WSL2 overriding this setting to make it 60 again?

3
  • No matter how much RAM, or the setting of vm.swappiness, some swap will almost always be used. If you only saw "a few blocks of swap utilized", then leave vm.swappiness alone.
    – heynnema
    Dec 6 '21 at 23:07
  • @heynnema Well I meant swap was utilized by that statement. However, I got enough ram(16 gigs, probably enough for my use case). Upon updating sysctl.conf and reloading it, free command showed swap used was 0. So that swappinness actually sort of worked. And I don't want this thing to constantly write under swap file and wear out my ssd. Dec 7 '21 at 8:07
  • 1
    If you watch free -h you'll still see some being used after a while, even though you've tweaked vm.swappiness. Modern SSDs won't wear out due to swap.
    – heynnema
    Dec 7 '21 at 15:04
5

/etc/sysctl.conf is usually parsed by Systemd in Ubuntu on startup (I believe via systemd-sysctl.service). Since WSL doesn't use Systemd, it doesn't get "handled" when you shut down and restart the WSL subsystem. This is similar to what I describe in my answer to this Stack Overflow question (no need to read it now, but it has more detail if you need it).

The solution (also as I describe in that particular answer) is to use the kernelCommandLine setting for WSL2's kernel:

  • In your Windows %userprofile% directory (typically C:\Users\<username>) create or edit the file .wslconfig with the following:

    [wsl2]
    kernelCommandLine = "sysctl.vm.swappiness=10"
    
  • Exit Ubuntu on WSL (and any other WSL instance)

  • Issue a wsl --shutdown from PowerShell, CMD, or the Start Menu

  • Restart

You should find that vm.swappiness is now persisted. Note that since all WSL2 sessions share the same kernel, this option will take effect globally. This is the case regardless of how you set the swappiness (or any kernel parameter).

Note also that this has been tested as far back as at least Windows 10 build 18363 by the OP of that Stack Overflow question.


Other alternatives

I believe the .wslconfig option is probably best for this, but you could also:

  • Use /etc/wsl.conf with the [boot] section on Windows 11 to force sysctl to run on each startup.
  • Use your user profile to manually run it. If you go this route, use something like wsl.exe -u root sysctl --system to avoid having to type a password or configure sudoers.
1
  • 1
    The .wslconfig fix works and was tested on windows 11! Awesome Dec 7 '21 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.