Understanding Memory Page Ins Page Outs

In order for a computer to work — it need a processor, memory, graphics processor and a storage attached to a motherboard. It’s incredible and it’s also very technical.

I am amazed by this little thing that works on computer — Memory. I always notice this thing call “Page Ins and Page Outs” but I never did try to understand it further. I google them but after many years, I just forgot about them again — bad memory!

I google them again today and I like to quote the answer by Bob Harris — he wrote a very long explanation and I like it. 

SSD is faster than machnical rotating hard disk, so pageout/pagein activity will also be faster. Not as fast as keeping everything in RAM, but software tends to be greedy and always get bigger as computers are made with more RAM. It seems to be a law of nature 🙂 


Inactive Memory is your cache of recently used code and data. The OS will check there before doing a more expensive I/O to disk (network I/O if it is network attacked storage). Also inactive memory maybe deferred writes to avoid constant waking up of the disk to do periodic writes that wastes battery life. It will consolidate a lot of little writes into larger writes bunched together in time so the disk can stay asleep for longer periods of time, saving power. 


If the OS needs RAM it can get it from free or inactive memory, as the are both available. However, if the inactive memory still has data to be written, the OS will need to do I/O first to save the data to disk, then it can reuse the RAM. 


Doing a purge just forces all the OS to clean its RAM cache, doing I/O writes if necessary, then put the pages on the free list. The good news, you have lots of free RAM which is great for starting a new app that needs lots of memory and the app or data it needs has not been accessed recently. And you just consolidated a lot of deferred writes in one disk wake up. Bad news, active programs will need to access disk to get data that may have been in the cache you just purged, active programs that were making multiple updates to the same page that was cached will have read it in from disk before continuing.


Depending on what you are about to do, the purge can be good, or it can be a waste. As mentioned, if about to start new apps, it can help those apps by getting the inactive memory cache cleanup out of the way, but that app had better be a very RAM greedy app to justify taking away the cashed pages of all the other running programs. 


If you are personally running purge manually every once in awhile, it really doesn’t matter. If you write a script or get a utility that does the purge automatically, that is not a good idea. An automatic purge is going to keep waking up the disk using battery life, it is going to cause apps that could have avoided I/O to wake up the disk to get there data. If you have an SSD, you are going to increase the write activity which will shorten the SSD life expectancy. SSD drives have a limited number of writes in them, and then cells die. The drives have complicated algorithms to replace cells that no longer work, but if you write to them enough, you will exhaust the spares. In normal consumer use this is not a problem, but automatic purging just to keep a large free list, and the cache empty is not going to help an SSD. 


Again, you doing it manually when you know you are going to start a big app is not a problem, or doing it for fun to see what happens is not a problem. Just do not automate the purge if you own an SSD or a laptop (battery life). 


Pagein will not match pageout, as the OS will load the code for a program by paging it into RAM, and since it does not modify the program code, it does not need to pageout the code when done with it. The OS can just forget it and page it back in the next time it needs that code. And paged out memory may not be needed again when a program quits, so it can be forgotten and not paged in again. So do not try to balance pagein with pageout. 


With respect to why you would have pageouts if you have free RAM, the pageout/pagein counters are from boot time and you may have had some activity since you booted that did have a need for more RAM than you had at that time. Also, it is possible for a program to map a file into RAM and read it via pagein, then write to it by using pageouts. For most file I/O this is not efficient, but it is an option available to programmers, and a few use it. Pageout/pagein counters are not restricted to just the swapfiles, so this type of I/O will also increment them. 


And back to Lightroom app, when you quit, the inactive memory is caching Lightroom code and data in case you want to access it again. The OS does not know what you are going to do next. And the are programs that get quit and restarted frequently, so caching recently accessed code and data is a win for saving power hungry disk I/O and saving SSD writes. 


If you really want to get into the black art of disk I/O avoidance, caching, virtual memory, SSD lifetimes, battery savings, etc…, fire up Google and dive in 🙂

Bob Harris