static void FreeHGlobal(IntPtr hglobal);
[System.Security.SecurityCritical]
public static void FreeHGlobal (IntPtr hglobal);
public static void FreeHGlobal (IntPtr hglobal);
[<System.Security.SecurityCritical>]
static member FreeHGlobal : nativeint -> unit
static member FreeHGlobal : nativeint -> unit
Public Shared Sub FreeHGlobal (hglobal As IntPtr)

下列範例示範呼叫 FreeHGlobal 方法。 此程式代碼範例是提供給 Marshal 類別之較大範例的一部分。

// Demonstrate how to call GlobalAlloc and 
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal::AllocHGlobal(100);
Marshal::FreeHGlobal(hglobal);
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);
' Demonstrate how to call GlobalAlloc and 
' GlobalFree using the Marshal class.
Dim hglobal As IntPtr = Marshal.AllocHGlobal(100)
Marshal.FreeHGlobal(hglobal)

下列範例示範如何將Managed String 類別的內容轉換為 Unmanaged 記憶體,然後在完成時處置 Unmanaged 記憶體。

using namespace System;
using namespace System::Runtime::InteropServices;
#include <iostream>                                                 // for printf
int main()
    // Create a managed string.
    String^ managedString = "Hello unmanaged world (from the managed world).";
    // Marshal the managed string to unmanaged memory.
    char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();
    printf("stringPointer = %s\n", stringPointer);
    // Always free the unmanaged string.
    Marshal::FreeHGlobal(IntPtr(stringPointer));
    return 0;
using System;
using System.Runtime.InteropServices;
using System.Threading;
class MainFunction
    static void Main()
        Console.WriteLine("\nStringToGlobalAnsi\n");
        // Create a managed string.
        String  managedString = "I am a managed String";
        Console.WriteLine("1) managedString = " + managedString);
        // Marshal the managed string to unmanaged memory.
        IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
        Console.WriteLine("2) stringPointer = {0}", stringPointer);
        // Get the string back from unmanaged memory.
        String RetrievedString = Marshal.PtrToStringAnsi(stringPointer);
        Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString);
        // Always free the unmanaged string.
        Marshal.FreeHGlobal(stringPointer);
        // IntPtr handle value is still the same:
        Console.WriteLine("4) stringPointer = " + stringPointer);
        // However, the data may be cleared after the memory is freed, depending on whether the memory allocated to stringPointer
        // has been reclaimed or not. Uncommenting the following line (Thread.Sleep(1000)) increases the likelihood of the memory being reclaimed.
        // Thread.Sleep(1000);
        String RetrievedString2 = Marshal.PtrToStringAnsi(stringPointer);
        Console.WriteLine("5) RetrievedString2 = " + RetrievedString2);

這個原生記憶體配置器是舊版 API,在 Windows 平臺上由特定 Win32 API 呼叫時,應該獨佔使用。 以 .NET 6 或更新版本為目標時,請在所有平臺上使用 NativeMemory 類別來配置原生記憶體。 以 .NET 6 或更早版本為目標時,請在所有平臺上使用 AllocCoTaskMem 來配置原生記憶體。

您可以使用 FreeHGlobal,從 AllocHGlobalReAllocHGlobal或任何對等的 Unmanaged API 方法所配置的全域堆積中釋放任何記憶體。 如果 hglobal 參數 IntPtr.Zero 方法不會執行任何動作。

FreeHGlobal 會從 Kernel32.DLL 公開 LocalFree 函式,這會釋放所有位元組,讓您無法再使用 hglobal所指向的記憶體。

除了 FreeHGlobal之外,Marshal 類別還提供另外兩個記憶體解除分配 API 方法:DestroyStructureFreeCoTaskMem