Page size

From Wikipedia, the free encyclopedia

Page size refers the size of a memory block used by a processor architecture that supports paged memory. It also represents the smallest possible size of a memory allocation. For example, if a process requests to the operating system to allocate 64 bytes, but the page size is 4KB (as is the i386 class processor), then the operating system must allocate an entire page, or 4KB to the process. This seems to be a great waste of memory, but normally processes do not do their own page allocations but instead use a memory manager to allocate pages which then they subdivide the into smaller, more useful allocations that do not waste memory. An example of this is the C runtime library's malloc. Thus, the most efficient use of memory is when allocations are a multiple of the page size.

Contents

[edit] Large Pages Versus Small Pages

Processor designers that implement paging often allow two or more, sometimes simultaneous, page sizes due to the global benefits or penalties of using a certain page size.

[edit] Overhead

Since the operating system must keep track of what pages have been allocated to which processes, having a smaller page size means that more entries must be kept, resulting in used memory. For example, if a machine had 4096 MB of RAM and the processor used 1MB pages, then exactly 4096 (4096MB / 1MB = 4096) pages of physical memory exist. Compare that to using 4KB pages: the operating system must keep track of 1048576 pages (4096MB / 4KB = 1048576), or about 1 million. Since these entries cannot be written out to disk, they consume memory that could otherwise be used for other things. For operating systems that traverse pages linearly, smaller pages also means a larger number of pages to traverse. When ever a page fault occurs and the page must be written to some physical medium, using a larger page size means increased activity per fault. However, due to the fact that this frees a large portion of memory for the process to use, hopefully a page fault will not occur soon, though in the case of thrashing, this degrades overall system performance due to the increased disk activity from larger pages.

[edit] Small allocations

As noted before, using a larger page size means that allocations are larger. Most processes use a memory manager rather than directly allocate their own pages, but even still, memory manages must allocate memory for use. If a the page size is 1MB and the process allocates 1025KB, the memory manager has no choice but to allocate 2MB of pages. Usually, the other 1023KB will be used by the memory manager in other allocation requests that are < 1023KB, but regardless of whether the memory is used or not, it has been allocated. Memory managers often do not release free pages back to the operating system once the process has requested them to be deallocated. This is due to the fact that it is often expensive in CPU time to allocate pages with the operating system's help, usually requiring a context switch to transition into kernel mode, where page allocation privileges exist. Instead, most memory managers merely mark the page as free so that they can reuse it the next time the process tries to allocate memory.

[edit] Determining the Page Size

Most operating systems allow programs to determine what the page size is so that they can allocate memory more efficiently.

[edit] UNIX and POSIX-based Operating Systems

UNIX and POSIX-based systems use the C function sysconf().


#include <unistd.h>

printf("The page size for this system is %ld bytes\n", sysconf(_SC_PAGESIZE)); //_SC_PAGE_SIZE is OK too.

[edit] Win32-based Operating Systems (Windows 9x, NT, ReactOS)

Win32-based operating system use the C function GetSystemInfo() function in kernel32.dll

#include <windows.h>

SYSTEM_INFO si;

GetSystemInfo(&si);
printf("The page size for this system is %u bytes\n", si.dwPageSize);