> Windows Syscalls
ntoskrnl.exeT1055T1027.009T1106

NtFreeVirtualMemory

Decommits or releases a region of virtual memory in a target process.

Prototype

NTSTATUS NtFreeVirtualMemory(
  HANDLE  ProcessHandle,
  PVOID  *BaseAddress,
  PSIZE_T RegionSize,
  ULONG   FreeType
);

Arguments

NameTypeDirDescription
ProcessHandleHANDLEinHandle to the target process. Use NtCurrentProcess() ((HANDLE)-1) for self.
BaseAddressPVOID*in/outPointer to the base address of the region. Updated to the actual base on return.
RegionSizePSIZE_Tin/outPointer to the region size in bytes. Must be 0 with MEM_RELEASE to free the entire reservation.
FreeTypeULONGinFree operation. MEM_DECOMMIT, MEM_RELEASE, or MEM_COALESCE_PLACEHOLDERS (Win10+).

Syscall IDs by Windows version

Windows versionSyscall IDBuild
Win10 15070x1Ewin10-1507
Win10 16070x1Ewin10-1607
Win10 17030x1Ewin10-1703
Win10 17090x1Ewin10-1709
Win10 18030x1Ewin10-1803
Win10 18090x1Ewin10-1809
Win10 19030x1Ewin10-1903
Win10 19090x1Ewin10-1909
Win10 20040x1Ewin10-2004
Win10 20H20x1Ewin10-20h2
Win10 21H10x1Ewin10-21h1
Win10 21H20x1Ewin10-21h2
Win10 22H20x1Ewin10-22h2
Win11 21H20x1Ewin11-21h2
Win11 22H20x1Ewin11-22h2
Win11 23H20x1Ewin11-23h2
Win11 24H20x1Ewin11-24h2
Server 20160x1Ewinserver-2016
Server 20190x1Ewinserver-2019
Server 20220x1Ewinserver-2022
Server 20250x1Ewinserver-2025

Kernel module

ntoskrnl.exeNtFreeVirtualMemory

Related APIs

VirtualFreeVirtualFreeExNtAllocateVirtualMemoryNtAllocateVirtualMemoryExNtProtectVirtualMemoryNtUnmapViewOfSection

Syscall stub

4C 8B D1            mov r10, rcx
B8 1E 00 00 00      mov eax, 0x1E
F6 04 25 08 03 FE 7F 01   test byte ptr [0x7FFE0308], 1
75 03               jne short +3
0F 05               syscall
C3                  ret
CD 2E               int 2Eh
C3                  ret

Undocumented notes

NtFreeVirtualMemory keeps SSN `0x1E` across every Windows 10 / 11 / Server build observed. It is the natural pair to NtAllocateVirtualMemory and dispatches through MmFreeVirtualMemory. MEM_DECOMMIT keeps the VAD reservation but releases physical pages; MEM_RELEASE tears down the reservation entirely (and requires RegionSize == 0 and BaseAddress to match the original allocation base).

Common malware usage

Used at the *end* of a payload's lifecycle rather than the beginning — wipe shellcode after execution so a follow-up memory scan finds zeroed or unmapped pages. The technique was popularised by `Hunt-Sleeping-Beacons`-style countermeasures: rather than leave RWX pages full of recognisable beacon code resident across long sleeps, modern loaders allocate, execute, then NtFreeVirtualMemory the region (or NtProtectVirtualMemory it back to PAGE_NOACCESS, encrypt, and re-protect on resume). Also used by hollowing implementations to free the original PEB-anchored image base mapping before remapping a malicious image.

Detection opportunities

Standalone calls are extremely noisy. Interesting patterns are short-lived MEM_COMMIT|EXECUTE → execute → MEM_RELEASE sequences (visible via ETW Threat Intelligence Memory events on Win10 2004+) and frees that target regions whose VAD shows recent PAGE_EXECUTE_READWRITE protection changes. Free events of the original image base in a process whose start image differs from its on-disk image are a strong process-hollowing tell.

Direct syscall examples

asmx64 direct stub

; Direct syscall stub for NtFreeVirtualMemory (SSN 0x1E, all builds)
NtFreeVirtualMemory PROC
    mov  r10, rcx          ; syscall convention
    mov  eax, 1Eh          ; SSN
    syscall
    ret
NtFreeVirtualMemory ENDP

cwipe-after-exec shellcode hygiene

// Allocate, run, then free — defeats post-mortem memory scans.
PVOID  base = NULL;
SIZE_T size = shellcode_len;
NtAllocateVirtualMemory(NtCurrentProcess(), &base, 0, &size,
                        MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memcpy(base, shellcode, shellcode_len);

ULONG old;
NtProtectVirtualMemory(NtCurrentProcess(), &base, &size,
                       PAGE_EXECUTE_READ, &old);
((void(*)(void))base)();

// Zero before unmap to defeat free-list residue grabs.
RtlSecureZeroMemory(base, shellcode_len);
SIZE_T zero = 0;
NtFreeVirtualMemory(NtCurrentProcess(), &base, &zero, MEM_RELEASE);

rustRAII wrapper around NtFreeVirtualMemory

// Cargo: ntapi = "0.4", windows-sys = "0.59"
use ntapi::ntmmapi::NtFreeVirtualMemory;
use windows_sys::Win32::System::Memory::MEM_RELEASE;

pub struct VirtualRegion { base: *mut core::ffi::c_void, len: usize }

impl Drop for VirtualRegion {
    fn drop(&mut self) {
        let mut base = self.base;
        let mut len: usize = 0; // MEM_RELEASE requires 0
        unsafe {
            NtFreeVirtualMemory(-1isize as _, &mut base, &mut len, MEM_RELEASE);
        }
    }
}

MITRE ATT&CK mappings

Last verified: 2026-05-20