# MallocTracked

> Allocates a block of memory with specified size, alignment, and tracking information.

## Definition

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

## MallocTracked(long, int, Allocator, int)

Allocates a block of memory with specified size, alignment, and tracking information.

```csharp
public static void* MallocTracked(long size, int alignment, Allocator allocator, int callstacksToSkip)
```

### Parameters

**** (\[long]\(https\://learn.microsoft.com/dotnet/api/system.int64)): The size of the memory block to allocate, in bytes. Ensure the size is sufficient for the intended data storage to avoid buffer overflow.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Specifies the alignment of the memory block. Alignment must be a power of two to ensure efficient memory access patterns for various processor architectures.**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The memory allocator used to manage the allocation. Choose an allocator type that matches the intended use case for the allocated memory, such as [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md) or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md).**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Specifies the number of call stack frames to skip when tracking memory. Adjust this to refine the granularity of stack traces collected for memory profiling.

### Returns

| Type                                                         | Description                                                                                                                                                       |
| ------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [void\*](https://learn.microsoft.com/dotnet/api/system.void) | A pointer to the allocated memory block. Manage this pointer diligently, ensuring it is freed appropriately to prevent memory leaks and accessing invalid memory. |

### Remarks

The `MallocTracked` method allocates memory while providing options to track memory allocations for debugging and profiling purposes. This function is similar to [UnsafeUtility.Malloc](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloc.md), but with additional tracking that helps identify memory allocations that might contribute to memory leaks or inefficiencies in performance-critical applications.

Ensure that the allocated memory is properly freed with [UnsafeUtility.Free](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/free.md) when it is no longer required to avoid memory leaks. Balance the `callstacksToSkip` parameter value to focus on your debugging needs while minimizing overhead.

See Also: [UnsafeUtility.FreeTracked](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/freetracked.md), [UnsafeUtility.Malloc](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloc.md), [UnsafeUtility.SetLeakDetectionMode](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/setleakdetectionmode.md).

### Examples

```csharp
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

public class MallocTrackedSimplifiedExample : MonoBehaviour
{
    void Start()
    {
        // Enable Native Leak Detection with stack trace for debugging
        UnsafeUtility.SetLeakDetectionMode(NativeLeakDetectionMode.EnabledWithStackTrace);

        // Allocate a block of memory for a CustomNativeArray
        Allocate(10, Allocator.Persistent, out CustomNativeArray<int> array);

        // NOTE: Not freeing the buffer in this example; this is intentional for demonstration.
        // During a Domain reload, such as entering/exiting Play Mode, a warning will appear
        // if memory is not properly freed, aiding in identifying leaks.
    }

    [StructLayout(LayoutKind.Sequential)]
    unsafe struct CustomNativeArray<T> where T : struct
    {
        public void* m_Buffer;
        public int m_Length;
        public Allocator m_AllocatorLabel;
    }

    static void Allocate<T>(int length, Allocator allocator, out CustomNativeArray<T> array) where T : struct
    {
        long totalSize = UnsafeUtility.SizeOf<T>() * length;
        array = default;

        unsafe
        {
            // Allocate memory with leak detection tracking
            array.m_Buffer = UnsafeUtility.MallocTracked(totalSize, UnsafeUtility.AlignOf<T>(), allocator, 0);
        }

        // Set the array metadata
        array.m_Length = length;
        array.m_AllocatorLabel = allocator;
    }
}
```

## MallocTracked(long, int, MemoryLabel, int)

Allocates a block of memory with specified size, alignment, memory label and tracking information.

```csharp
public static void* MallocTracked(long size, int alignment, MemoryLabel label, int callstacksToSkip)
```

### Parameters

**** (\[long]\(https\://learn.microsoft.com/dotnet/api/system.int64)): The size of the memory block to allocate, in bytes. Ensure the size is sufficient for the intended data storage to avoid buffer overflow.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Specifies the alignment of the memory block. Alignment must be a power of two to ensure efficient memory access patterns for various processor architectures.**** (\[MemoryLabel]\(/engine/6000.7/script-reference/unity/collections/memorylabel)): The memory label to allocate under.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Specifies the number of call stack frames to skip when tracking memory. Adjust this to refine the granularity of stack traces collected for memory profiling.

### Returns

| Type                                                         | Description                                                                                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ |
| [void\*](https://learn.microsoft.com/dotnet/api/system.void) | A pointer to the allocated memory block. Manage this pointer carefully to prevent memory leaks and ensure proper deallocation. |

### Remarks

The `MallocTracked` method allocates memory while providing options to track memory allocations for debugging and profiling purposes. This function is similar to [UnsafeUtility.Malloc](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloc.md), but with additional tracking that helps identify memory allocations that might contribute to memory leaks or inefficiencies in performance-critical applications.

Specifying a memory label helps in profiling and debugging memory usage in Unity.

To avoid memory leaks, free the allocated memory with [UnsafeUtility.Free](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/free.md) when it's no longer required. Balance the `callstacksToSkip` parameter value to focus on your debugging needs while minimizing overhead.

See Also: [MemoryLabel](/engine/6000.7/script-reference/unity/collections/memorylabel.md), [UnsafeUtility.FreeTracked](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/freetracked.md), [UnsafeUtility.Malloc](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloc.md), [UnsafeUtility.SetLeakDetectionMode](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/setleakdetectionmode.md).
