This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
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 theVirtualAllocEx function, use theVirtualFreeEx function.
BOOL VirtualFree( [in] LPVOID lpAddress, [in] SIZE_T dwSize, [in] DWORD dwFreeType);[in] lpAddress
A pointer to the base address of the region of pages to be freed.
If thedwFreeType parameter isMEM_RELEASE, this parameter must be the base address returned by theVirtualAlloc function when the region of pages is reserved.
[in] dwSize
The size of the region of memory to be freed, in bytes.
If thedwFreeType parameter isMEM_RELEASE, this parameter must be 0 (zero). The function frees the entire region that is reserved in the initial allocation call toVirtualAlloc.
If thedwFreeType parameter isMEM_DECOMMIT, the function decommits all memory pages that contain one or more bytes in the range from thelpAddress parameter to(lpAddress+dwSize). This means, for example, that a 2-byte region of memory that straddles a page boundary causes both pages to be decommitted. IflpAddress is the base address returned byVirtualAlloc anddwSize is 0 (zero), the function decommits the entire region that is allocated byVirtualAlloc. After that, the entire region is in the reserved state.
[in] dwFreeType
The type of free operation. This parameter must be one of the following values.
| Value | Meaning |
|---|---|
| Decommits the specified region of committed pages. After the operation, the pages are in the reserved state. The function does not fail if you attempt to decommit an uncommitted page. This means that you can decommit a range of pages without first determining the current commitment state. TheMEM_DECOMMIT value is not supported when thelpAddress parameter provides the base address for an enclave. This is true for enclaves that do not support dynamic memory management (i.e. SGX1). SGX2 enclaves permitMEM_DECOMMIT anywhere in the enclave. |
| Releases the specified region of pages, or placeholder (for a placeholder, the address space is released and available for other allocations). After this operation, the pages are in the free state. If you specify this value,dwSize must be 0 (zero), andlpAddress must point to the base address returned by theVirtualAlloc function when the region is reserved. The function fails if either of these conditions is not met. If any pages in the region are committed currently, the function first decommits, and then releases them. The function does not fail if you attempt to release pages that are in different states, some reserved and some committed. This means that you can release a range of pages without first determining the current commitment state. |
When usingMEM_RELEASE, this parameter can additionally specify one of the following values.
| Value | Meaning |
|---|---|
| To coalesce two adjacent placeholders, specifyMEM_RELEASE | MEM_COALESCE_PLACEHOLDERS. When you coalesce placeholders,lpAddress anddwSize must exactly match the overall range of the placeholders to be merged. |
| Frees an allocation back to a placeholder (after you've replaced a placeholder with a private allocation usingVirtualAlloc2 orVirtual2AllocFromApp). To split a placeholder into two placeholders, specify |
If the function succeeds, the return value is nonzero.
If the function fails, the return value is 0 (zero). To get extended error information, callGetLastError.
Each page of memory in a process virtual address space has aPage State. TheVirtualFree function can decommit a range of pages that are in different states, some committed and some uncommitted. This means that you can decommit a range of pages without first determining the current commitment state of each page. 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. Subsequently, you can callVirtualAlloc to commit it, orVirtualFree to release it. Attempts to read from or write to a reserved page results in an access violation exception.
TheVirtualFree function can release a range of pages that are in different states, some reserved and some committed. This means that you can release a range of pages without first determining the current commitment state of each page. The entire range of pages originally reserved by theVirtualAlloc function must be released at the same time.
If a page is released, its state changes to free, and it is available for subsequent allocation operations. After memory is released or decommited, you can never refer to the memory again. Any information that may have been in that memory is gone forever. Attempting to read from or write to a free page results in an access violation exception. If you need to keep information, do not decommit or free memory that contains the information.
TheVirtualFree function can be used on an AWE region of memory, and it invalidates any physical page mappings in the region when freeing the address space. However, the physical page is not deleted, and the application can use them. The application must explicitly callFreeUserPhysicalPages to free the physical pages. When the process is terminated, all resources are cleaned up automatically.
Windows 10, version 1709 and later and Windows 11: To delete the enclave when you finish using it, callDeleteEnclave. You cannot delete a VBS enclave by calling theVirtualFree orVirtualFreeEx function. You can still delete an SGX enclave by callingVirtualFree orVirtualFreeEx.
Windows 10, version 1507, Windows 10, version 1511, Windows 10, version 1607 and Windows 10, version 1703: To delete the enclave when you finish using it, call theVirtualFree orVirtualFreeEx function and specify the following values:
For an example, seeReserving and Committing Memory.
| Requirement | Value |
|---|---|
| Minimum supported client | Windows XP [desktop apps | UWP apps] |
| Minimum supported server | Windows Server 2003 [desktop apps | UWP apps] |
| Target Platform | Windows |
| Header | memoryapi.h (include Windows.h, Memoryapi.h) |
| Library | onecore.lib |
| DLL | Kernel32.dll |
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?