0

I searched with “Why do I need a swapfile?” and “Why does a computer need a swapfile/” and the first page of results didn’t tell me anything technical. Rather to turn it off or to not use it in high RAM circumanstances or how to actually turn it on.

I was told once for a Windows machine that programs dimension large portions of memory and with more processess the computer needs a swap file since all or many of those processess do ask the system for large memory sizes but in the end don’t use it very much. Like the whole webpage in RAM for a browser and like 100’s tabs opened.

But I sense a bit that is not correct and even not true.

Can someone explain to me why a computer — and I hope it will be a Windows or Linux computer answer — needs swap file?

My system just went down to 125MB again and got slow and now I used swap on and now it just works? How is that possible? Checking top says I have 8GB swap now (partition) and I needed to killall my browser (many many tabs) enabled swap with swap on, restarted browser, opened all the tabs again but no slow down? On top of that, top (the process) says computer uses 0KB swap?

Giacomo1968
  • 58,727

3 Answers3

0

You might not be "swapping" data out to the swap file, but it is providing an extra backing store for when you might.

The problem is that programs will often over-allocate RAM on the assumption that you do have a swap file. One feature/problem of Windows is that in allowing programs to allocate more than they actually use they can have a different quantity of actual (working) memory compared to what the system is committed to be able to provide.

You might have a working set of 4GB across all your programs, but because all the processes over-allocate memory, the actual commit charge could be 8 or even 16GB. When reading large files it is common practice to tell the operating system to simply "map" the entire file into your own address space (see Memory Mapped I/O) and this effectively appears in your processes memory space. That space needs "commitment" in the virtual memory system, and that commitment needs to be backed by storage in the system in case you start writing to that memory but not the file itself.

A good proportion of committed RAM might well be "empty" but Windows wants to know that it can fulfil that commitment. As a result you hit an unseen memory limit while still having memory "free". This failure to allocate more virtual memory (different from RAM) is what is causing the error you get.

The swap file allows Windows to say "well, if programs do want more memory then I can push some out to disk" and allows more programs to over-allocate RAM making better use of what you actually have.

While the working set is still below your total RAM, then Windows or Linux will likely not push data out to the page file, but it knows that if it needs to then it could.

By turning off the page file you are causing your machine to be memory constrained, even when you are not.

Linux should apparently allow memory overcommitment, but Windows is much less willing to allow it.

Mokubai
  • 95,412
0

It doesn’t. However, what you read is actually accurate: Programs may reserve more memory than they need. How much more they use (if any) varies wildly. The page file (on Windows) is a mechanism to back these reservations from HDD/SSD space rather than wasting physical RAM for them.

Windows and Linux treat virtual memory differently. On Linux, it is possible to “overcommit”: To reserve more memory than available. On Windows, this is not possible. Once all physical RAM and page file space is reserved, further reservations will not be possible.

On Windows, if you do not have a page file, all reservations must be backed by physical RAM. Especially if you don’t have a lot of RAM (16 GiB+ today) you’ll quickly encounter out-of-memory situations. I just booted 2 hours ago, browsed a little and watched a movie and already software has reserved more than 8 GiB or memory on my PC. (Just over 5 GiB are actually active.)

I cannot accurately comment on your browser situation, but one thing’s for sure: By restarting your browser, you basically made sure all leftover reservations were removed and memory usage is minimized. It will grow again as you continue to use it.

If you want to further research this topic, you’ll have to read up on how virtual memory works. (One thing to keep in mind is that virtual memory is an integral part of modern computing and cannot be disabled on high-level operating systems. Often you’ll find stuff that basically goes like “disabling the page file is disabling virtual memory”. This is entirely wrong.)

user219095
  • 65,551
0

Modern CPUs have an ability called paging.

CPUs that can do paging have a component called an MMU that can remap RAM and also have "page faults".

The paging mechanism splits RAM into small sections called pages (typically 4Kbytes). Combined with page faults, this enables the following:

  • This allows the operating to assign pages to processes and arrange pages so a process thinks it has a continuous section of memory (when in reality it may be all over the place).

  • It also allows a page fault to happen when a page is accessed for various reasons. A page fault is a "CPU exception" that immediately transfers control back to the OS. One very common reason is to control the amount of RAM a process can use. If a process tries to access a page that's "higher" than the memory given to it by OS, a page fault will happen and the OS can then terminate that process for trying to access memory that doesn't belong to it.

So a third thing that's possible - the OS can do the following:

  • Save a page that might belong to a process not being used to disk, and mark it as on-disk.

  • When the process is called back for any reason, a page fault will happen, and the CPU can bring the saved contents back from disk. Control is then returned to the process and it doesn't even know it was "paged out" to disk.

This is why there is a swapfile, or paging file as it's also called.

It lets your operating system be more efficient by moving processes that haven't been used in a while out of RAM, to allow more RAM to be available for programs you are actually using.

It can also give some buffer (your computer will slow down instead of programs refusing to start) if you have more processes + memory requests active than you actually have RAM for.

You don't really need a swapfile/paging file if you know for a fact that everything you ever will want to do with all programs you will run at once will fit into RAM, but it's difficult to know that for sure.

LawrenceC
  • 75,182