You can use Marshal.FreeCoTaskMem(IntPtr) to free any memory allocated by Marshal.AllocCoTaskMem(int), Marshal.ReAllocCoTaskMem(IntPtr, int), or any equivalent unmanaged method. If the ptr parameter is null, the method does nothing.
Marshal.FreeCoTaskMem(IntPtr) exposes the COM tp://go.microsoft.com/fwlink/?LinkId=148638 function, which frees all bytes so that you can no longer use the memory that the ptr parameter points to.
In addition to Marshal.FreeCoTaskMem(IntPtr), the System.Runtime.InteropServices.Marshal class provides two other memory-deallocation methods: Marshal.DestroyStructure(IntPtr, Type) and Marshal.FreeHGlobal(IntPtr).