VirtualLock
Applies to: desktop apps only
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.
Syntax
BOOL WINAPI VirtualLock( __in LPVOID lpAddress, __in SIZE_T dwSize );
Parameters
-
lpAddress [in]
-
指向要锁住的页区域的基地址指针.
dwSize [in]
-
The size of the region to be locked, in bytes. The region of affected pages includes all pages that contain one or more bytes in the range from the lpAddress parameter to
(lpAddress+dwSize)
. This means that a 2-byte range straddling a page boundary causes both pages to be locked.
Return value
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.
Remarks
在指定区域内的所有页必须被提交,用PAGE_NOACCESS保护的内存不能被锁住。
把页存到内存中会降低系统的表现性能,因为减少了可用的RAM并强制系统交给其它的关键页给分页文件. Each version of Windows has a limit on the maximum number of pages a process can lock. This 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. These pages are guaranteed not to be written to the pagefile while they are locked.
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.