# ReleaseGCObject(ulong)

> Releases a GC handle obtained from UnsafeUtility.PinGCObjectAndGetAddress or UnsafeUtility.PinGCArrayAndGetDataAddress.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Collections.LowLevel.Unsafe](/engine/6000.0/script-reference/unity/collections/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public static void ReleaseGCObject(ulong gcHandle)
```

### Parameters

**** (\[ulong]\(https\://learn.microsoft.com/dotnet/api/system.uint64)): The handle associated with the pinned object.

### Remarks

The `ReleaseGCObject` method unpins an object, allowing the garbage collector to manage its memory freely. Use this method to prevent resource leaks. Ensure you release each handle after finishing direct memory operations.

```csharp
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

public class ReleaseGCObjectExample : MonoBehaviour
{
    private struct MyStruct
    {
        public int value;
    }

    void Start()
    {
        // Box a struct
        object boxedStruct = new MyStruct { value = 10 };

        // Pin the object and get a GC handle
        unsafe
        {
            void* dataPtr = UnsafeUtility.PinGCObjectAndGetAddress(boxedStruct, out ulong gcHandle);

            // Access and modify data
            int objectHeaderSize = UnsafeUtility.SizeOf<long>() * 2;
            int* valuePtr = (int*)((byte*)dataPtr + objectHeaderSize);
            *valuePtr = 20;

            // Release the GC handle
            UnsafeUtility.ReleaseGCObject(gcHandle);
        }

        // Verify modification
        MyStruct modifiedStruct = (MyStruct)boxedStruct;
        Debug.Log($"Modified value: {modifiedStruct.value}"); // Outputs: 20

        // Confirm handle release
        Debug.Log("GC handle released.");
    }
}
```

Additional Resources: [UnsafeUtility.PinGCObjectAndGetAddress](/engine/6000.0/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/pingcobjectandgetaddress.md), [UnsafeUtility.PinGCArrayAndGetDataAddress](/engine/6000.0/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/pingcarrayandgetdataaddress.md).
