Score:4

Swappiness on WSL2 gets reset to 60

cn flag

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?

heynnema avatar
ru flag
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.
Deekshith Anand avatar
cn flag
@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.
heynnema avatar
ru flag
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.
Score:5
vn flag

/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.
Deekshith Anand avatar
cn flag
The .wslconfig fix works and was tested on windows 11! Awesome
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.