wsl2 config is ignored

Viewed 606

I am using wsl and the problem is it loves ram. I tried to limit it by adding %USERPROFILE%/.wslconfig

this is what the file looks like:

[wsl2]
memory=2GB
swap=0
processors=2

and yet the Vmmem process happily passes that memory limit by a lot. I tried wsl --shutdown and even restarting the computer. none helped

2 Answers

I've had some issues troubleshooting why, suddenly, my .wslconfig stopped working. A few things to consider:

  • confirm that the filename is, in fact, ".wslconfig" and not something like ".wslconfig.txt" - make sure you're not hiding filename extensions, and rename as necessary
  • confirm that the .wslconfig file is saved as UTF-8 and not UTF-8-BOM (I use notepad++ and use the "Encoding" menu to see and change as necessary)
  • confirm that the top of the file begins with [wsl2]
  • if you're specifying the swapfile= parameter, make sure that:
    • the path already exists
    • the path needs to be added with double back-slashes, ie: C:\\Users\\Username\\AppData\\Local\\Temp\\swap.vhdx
    • make sure you specify the filename of the swapfile (ie, swap.vhdx)
    • make sure that none of the directories in your path are set to compress (this was an issue specifically on my machine). You can right-click on the folder/directory that is compressed (the icon on the folder in Windows Explorer will indicate it's a compressed folder) and uncheck the compress files and folders in the Advanced section.
  • if you're specifying the memory and/or swap size, make sure that the value ends with "GB" and not "gb" or "Gb", ie - memory=2GB or swap=16GB
    • apparently, swap size cannot exceed 255GB, the top limit that appears to be respected is swap=250GB anything more than that just results in this parameter being entirely ignored.

In cases where you may need more than 256GB of swap, there is an inelegant solution that I've occasionally used (since I don't often need more than 16GB of swap), you will need to specify a swapfile inside the WSL2 shell as an additional swapfile space. The problem is that the mounted swapfile is not persistent, and also doesn't work in /etc/fstab or in /etc/wsl.conf (I've tried automounting, and it just doesn't work with swap files). Here are the commands:

  1. In your WSL2 shell: sudo fallocate -l 256G /swapfile (in this example, it's 256GB - notice that it ends in just a "G" and not "GB" like above) - this will create a file in the root called "swapfile" that is 256GB large.
  2. Adjust the perms on the swapfile: sudo chmod 600 /swapfile
  3. Then type: sudo swapon /swapfile - this will mount the swapfile as a swap space on the system
  4. Then type free -m or swapon --show to see if the swapfile was mounted and available to the system.

Good luck to anyone who comes across this as it took me pouring over dozens of related posts to suss out some of these details.

Check your filename , make sure it is strictly ".wslconfig" , not like ".wslconfig.txt" , I faced this problem and when I checked using cmd I found out my file is saved as ".txt" . Few important points to note:

  1. File Name should be exactly ".wslconfig"
  2. File should be placed in %USERPROFILE% directory
  3. After putting file, try restarting wsl

You can read more about it here: https://docs.microsoft.com/en-us/windows/wsl/wsl-config

Related