Platform SDK: Memory |
The VirtualLock function locks the specified region of the process's virtual address space into physical memory, ensuring that subsequent access to the region will not incur a page fault.
BOOL VirtualLock( LPVOID lpAddress, // first byte in range SIZE_T dwSize // number of bytes in range );
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
All pages in the specified region must be committed. Memory protected with PAGE_NOACCESS cannot be locked.
Locking pages into memory may degrade the performance of the system by reducing the available RAM and forcing the system to swap out other critical pages to the paging file. By default, a process can lock a maximum of 30 pages. The default limit is intentionally small to avoid severe performance degradation. Applications that need to lock larger numbers of pages must first call the SetProcessWorkingSetSize function to increase their minimum and maximum working set sizes. The maximum number of pages that a process can lock is equal to the number of pages in its minimum working set minus a small overhead.
Pages that a process has locked remain in physical memory until the process unlocks them or terminates.
To unlock a region of locked pages, use the VirtualUnlock function. Locked pages are automatically unlocked when the process terminates.
This function is not like the GlobalLock or LocalLock function in that it does not increment a lock count and translate a handle into a pointer. There is no lock count for virtual pages, so multiple calls to the VirtualUnlock function are never required to unlock a region of pages.
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Unsupported.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Memory Management Overview, Memory Management Functions, SetProcessWorkingSetSize, VirtualUnlock