WSL: Initializing WSL 2 with more than 250GB of swap space fails

Environment

Windows build number: 10.0.20150.0

Your Distribution version: (Debian) Linux version 4.19.121-microsoft-WSL2-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Thu May 14 20:25:24 UTC 2020 Whether the issue is on WSL 2 and/or WSL 1: WSL 2

Steps to reproduce

  1. Create a .wslconfig file with the following items.

Note that drive E is an empty drive with around 512GB of free space.

[wsl2]

memory=8GB
swap=256GB
swapFile=E:\\swap.vhdx
  1. In PowerShell run, wsl --shutdown Debian

  2. Open up wsl and type in free -m

Expected behavior

free -m should report something similar to this.

              total        used        free      shared  buff/cache   available
Mem:           7972         256        7655           0          59        7556
Swap:        256000           0      256000

Actual behavior

However actually it reports this.

              total        used        free      shared  buff/cache   available
Mem:           7972         256        7655           0          59        7556
Swap:          0            0          0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Any update here? When using wsl2 +gpu for ml matrix calc, I bought 128GB(64x4) memory but need 256GB+ more memory. On hard linux machine I can set swap with 512GB(one entire ssd), but cannot set on wsl2?

Workaround for adding more Swap file

.wslconfig

[wsl2]
kernelCommandLine = vsyscall=emulate
processors=2
memory=2GB
swap=250GB
swapFile=c:\\wslswapfile.vhdx

WSL2 will auto create swapfile.vhdx with maximum 250GB in size (preset but not allocated dynamic vhdx disk) For create more swap than 250GB limit use trick below Install WSL2 Preview from Microsoft Store first: https://www.microsoft.com/store/productId/9P9TQF7MRM4R

  1. .wslconfig with swap=250GB and correct swapFile path, save
  2. Powershell: wsl --shutdown
  3. delete .vhdx file in path
  4. PS: start wsl by typing wsl.exe again & exit
  5. “Duplicate” (copy-paste) new created wslswapfile.vhdx to wslswapfile2.vhdx or download zipped file below
  6. PS: wsl --shutdown again
  7. start wsl by enter your distro bash shell (ex: Ubuntu)
  8. bash: wsl.exe --mount --vhd c:\wslswapfile.vhdx --name newswap (command from here: https://docs.microsoft.com/en-us/windows/wsl/wsl2-mount-disk)
  9. bash: sudo swapon /mnt/wsl/newswap/file
  10. bash: free -mh to check new swap applied
  11. repeat procedure above many times until meet your SWAP size need. Current linux kernel support up to 35 Swap Files Notes: all swap files mounted will lose after restart WSL, so we need run command mount and swapon every WSL start or create a startup script for that

Your can download .vhdx file below and unzip it for using as Swap file wslswapfile.zip

image

@therealkenc the inability to assign large amount of virtual memory to WSL2 continues to be an issue on current builds (including Windows 11). I’ve been able to fully reproduce the behavior described by the original author. Can we get this issue reopened?

That is very strange. I’m also running Windows 11 and have it working with a specified swapfile. My working .wslconfig file:


[wsl2]
memory=96GB
swap=128GB
swapFile=%USERPROFILE%\AppData\Local\Temp\swap.vhdx


(placed in my %UserProfile%)