1

What makes the virtual memory expand and what makes virtual memory contract, if possible?

Trying to understand the uses of virtual memory and what makes it expand and contract in Windows 11

5
  • 1
    In which operating system?
    – squillman
    Commented Feb 28 at 14:25
  • @squillman Windows 11 Commented Feb 28 at 14:43
  • are you asking about the size of the pagefile, or about the amount of it that contains page data at any given time? Commented Feb 28 at 14:50
  • @FrankThomas both Commented Feb 28 at 14:57
  • The pagefile (aka swap) (and its contents) should not be called "virtual memory". Such a conflation leads to odd questions like your title, and various misunderstandings. See superuser.com/questions/748743/…
    – sawdust
    Commented Feb 28 at 22:00

2 Answers 2

0

Virtual memory is usually pre-allocated and contiguous on the disk, as it needs to be fast.

Under Windows, the size of virtual memory is defined via the settings of Initial and Maximum. Changing this definition causes the page-file to be reallocated on the disk to the specified Initial size after a reboot.

If more virtual memory was required than is allocated, the page-file may need to be increased, but it cannot be reallocated on the disk to a bigger size because it already contains data, so extensions are added to it which might not be contiguous.

Once Windows is rebooted, its size returns to the Initial size, but not immediately. Windows will keep the extended page-file for some time, and will shrink it back to the Initial size when it decides that a larger file is no longer required.

For a user testimony, see this answer.

5
  • Alright, does it shrink when its automatically managed by Windows? also, how long is could "sometime" be, estimate? Commented Feb 28 at 14:59
  • It cannot shrink while Windows is running, since this will require finding out which parts of it contain data and which don't. Windows takes the simpler solution of increasing it when running, but reducing it only after a reboot, meaning when no jobs are running that can use any part of it. The algorithm behind the "how long" part is not documented, since Microsoft keeps the option of changing it in future Windows versions without notice. We don't know if this is by the number of boots or by the elapsed time.
    – harrymc
    Commented Feb 28 at 15:05
  • Alright, will it still have a chance of adjusting while in sleep or only rebooting does it? Commented Feb 28 at 15:07
  • Sleep keeps all the memory data of Windows, so the computer will wake up to the exact same state, which also includes any swapped memory. So sleep is not enough, a reboot is required.
    – harrymc
    Commented Feb 28 at 15:10
  • alright, thanks for your input. Commented Feb 28 at 15:24
0

In Windows, it's up to the user. The default on install has been to allow the Initial size and Maximum size to differ, so pagefile.sys continually grows and shrinks.

Personally, I prefer to set the pagefile.sys to a constant size to reduce fragmentation by setting Initial size and Maximum size to the same value.

And then there is swapfile.sys, which I believe is set to a fixed size.

1
  • Thanks, I looked at those settings. Commented Feb 28 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .