Platform SDK: Memory

VirtualFree

The VirtualFree function releases, decommits, or releases and decommits a region of pages within the virtual address space of the calling process.

To free memory allocated in another process by the VirtualAllocEx function, use the VirtualFreeEx function.

BOOL VirtualFree(
  LPVOID lpAddress,   // address of region
  SIZE_T dwSize,      // size of region
  DWORD dwFreeType    // operation type
);

Parameters

lpAddress
[in] Pointer to the base address of the region of pages to be freed. If the dwFreeType parameter includes MEM_RELEASE, this parameter must be the base address returned by the VirtualAlloc function when the region of pages was reserved.
dwSize
[in] Specifies the size, in bytes, of the region to be freed. If the dwFreeType parameter includes MEM_RELEASE, this parameter must be zero. Otherwise, the region of affected pages includes all pages containing 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 freed.
dwFreeType
[in] Specifies the type of free operation. This parameter can be one, or both, of the following values.
Value Meaning
MEM_DECOMMIT Decommits the specified region of committed pages.

An attempt to decommit an uncommitted page will not cause the function to fail. This means that a range of committed or uncommitted pages can be decommitted without having to worry about a failure.

MEM_RELEASE Releases the specified region of reserved pages. If this value is specified, the dwSize parameter must be zero, or the function fails.

Return Values

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

VirtualFree can perform one of the following operations:

Pages that have been released are free pages available for subsequent allocation operations. Attempting to read from or write to a free page results in an access violation exception.

VirtualFree can decommit an uncommitted page; this means that a range of committed or uncommitted pages can be decommitted without having to worry about a failure. Decommitting a page releases its physical storage, either in memory or in the paging file on disk. If a page is decommitted but not released, its state changes to reserved, and it can be committed again by a subsequent call to VirtualAlloc. Attempting to read from or write to a reserved page results in an access violation exception.

The current state of the entire range of pages must be compatible with the type of free operation specified by the dwFreeType parameter. Otherwise, the function fails and no pages are released or decommitted.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

Memory Management Overview, Memory Management Functions, VirtualFreeEx