MSDN Documentation

Memory Management

This section provides detailed documentation on the core Windows APIs for managing memory. Effective memory management is crucial for application performance and stability. Understanding these APIs allows developers to allocate, deallocate, protect, and query memory regions.

Key Concepts

  • Virtual Memory: Windows uses a virtual memory system, which provides each process with its own private address space. This abstraction simplifies memory management for developers and enhances security by isolating processes.
  • Memory Allocation: APIs like VirtualAlloc, HeapAlloc, and malloc are used to request memory from the operating system.
  • Memory Deallocation: Functions such as VirtualFree, HeapFree, and free are used to release allocated memory back to the system.
  • Memory Protection: APIs like VirtualProtect allow control over the read, write, and execute permissions of memory pages.
  • Memory Mapping: Features like memory-mapped files enable efficient data sharing between processes and direct file access.

Core Functions and Structures

Here are some of the most commonly used APIs related to memory management:

  • VirtualAlloc: Reserves or commits a region of pages in the virtual address space of the calling process.
    LPVOID VirtualAlloc(
      LPVOID lpAddress,
      SIZE_T dwSize,
      DWORD  flAllocationType,
      DWORD  flProtect
    );
  • VirtualFree: Releases, decommits, or both a region of pages in the process's virtual address space, returning it to the system.
    BOOL VirtualFree(
      LPVOID lpAddress,
      SIZE_T dwSize,
      DWORD  dwFreeType
    );
  • VirtualQuery: Retrieves detailed information about a region of pages within the virtual address space of the calling process.
    SIZE_T VirtualQuery(
      LPCVOID                   lpAddress,
      PMEMORY_BASIC_INFORMATION lpBuffer,
      SIZE_T                    dwLength
    );
  • HeapAlloc: Allocates a block of memory from a specified heap.
    LPVOID HeapAlloc(
      HANDLE hHeap,
      DWORD  dwFlags,
      SIZE_T dwBytes
    );
  • HeapFree: Frees a specified block of memory that was previously allocated from a heap by calling the HeapAlloc function.
    BOOL HeapFree(
      HANDLE hHeap,
      DWORD  dwFlags,
      LPVOID lpMem
    );
  • GlobalAlloc: Allocates the specified number of bytes from the heap. The function returns a handle to the newly allocated memory.
    HGLOBAL GlobalAlloc(
      UINT   uFlags,
      SIZE_T dwBytes
    );
  • GlobalFree: Releases the specified global memory object and invalidates its handle.
    HGLOBAL GlobalFree(
      HGLOBAL hMem
    );
  • LocalAlloc: Allocates the specified number of bytes from the local memory heap. The function returns a handle to the newly allocated memory.
    HLOCAL LocalAlloc(
      UINT   uFlags,
      SIZE_T uBytes
    );
  • LocalFree: Frees the specified local memory object and invalidates its handle.
    HLOCAL LocalFree(
      HLOCAL hMem
    );

Related Topics

For more in-depth information, please refer to the official Windows Internals documentation.