venus.w
venus.w

Reputation: 2251

virtual memory page replacement

It is known that some pages need to be replaced in the demand of paging when all frames are full.However, pages can be used as text, rodata, data, which are all called in from disk I/O, but pages such stack, heap of a program are allocated in the memory only, no copy in the disk.I want to know whether pages of stack and heap are involved in page replacement, if so, should them be swapped into the disk temporarily? Otherwise, should they be pinned in the memory? Maybe this question is closely related to concrete implementation,and different strategy can be adopted,but I am eager to know the usual practice.

Upvotes: 0

Views: 980

Answers (2)

SecurityMatt
SecurityMatt

Reputation: 6743

All user-mode pages are eligable to be removed from RAM. Pages which are unmodified and backed by real data on disk (such as the executable and readonly parts of DLLs and EXEs) are simply removed from memory and demand-paged back in. All other pages are written to the pagefile and demand-paged back in. This includes stack, heap and VirtualAlloc'd memory.

Stack memory close to an active thread tends not to get paged out because it has been accessed recently, and hence is a bad choice to be paged out by the paging-daemon/APC, but the pages far up the callstack and on threads that are suspended often are paged out by the system, and are certainly not pinned in memory.

The only general exception to this is kernel stacks. Every core must have at least one kernel stack that is never paged out, because otherwise a page-fault would cause the processor to triple fault and the core to reboot.

Additionally both Linux and Windows have a concept of a "non-paged-pool", which is heap memory that will never be paged out. This contrasts with "paged-pool" memory which is paged out and in by the paging daemon as required. (This "non-paged" memory is required to service the page-fault daemon itself, core system services and services that are either required for the paging daemon to work such as filesystem drivers, the memory manager and so on)

Upvotes: 5

David Schwartz
David Schwartz

Reputation: 182743

That's why you need a swap or paging file. If you have pages that are dirty (the copy in memory has been modified) but haven't been accessed in a long time, the only way you can use the physical memory they occupy for something more useful is to first write them out to disk.

Clean pages (that contain only zeroes, data nobody could care about, or copies of files stored on disk) can simply be discarded from memory and then used for some other purpose.

Generally, the only pages that are pinned in memory are pages that are used by the kernel or pages that have been pinned by privileged processes. Many kernel functions pin things in memory temporarily so they can access them without fear of a page fault. These pages are unpinned before the kernel returns to user space.

This is a generic answer that should pretty much apply equally well to all mainstream modern general-purpose operating systems.

Upvotes: 2

Related Questions