# ProfilerRecorder

> Records the Profiler metric data that a Profiler marker or counter produces.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Profiling](/engine/6000.5/script-reference/unity/profiling.md)
* **Assembly:** UnityEngine.CoreModule
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public struct ProfilerRecorder : IDisposable
```

## Remarks

Use ProfilerRecorder to access performance metrics that the Profiler exposes. You can use it to read Profiler counter data such as memory or render statistics, and Profiler marker timing data in a uniform way.

You can use this API in Editor and Player builds, including Release Players. Use [ProfilerRecorderHandle.GetAvailable](/engine/6000.5/script-reference/unity/profiling/lowlevel/unsafe/profilerrecorderhandle/getavailable.md) to get the full list of supported metrics. For a list of built-in Profiler markers and counters available, refer to [Profiler markers reference](/engine/6000.5/manual/analysis/profiler/markers.md) and [Profiler counters reference](/engine/6000.5/manual/analysis/profiler/counters-reference.md).

The following example demonstrates how you can use ProfilerRecorder to get memory and timing statistics.

```csharp
using System.Collections.Generic;
using System.Text;
using Unity.Profiling;
using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    string statsText;
    ProfilerRecorder systemMemoryRecorder;
    ProfilerRecorder gcMemoryRecorder;
    ProfilerRecorder mainThreadTimeRecorder;

    static double GetRecorderFrameAverage(ProfilerRecorder recorder)
    {
        var samplesCount = recorder.Capacity;
        if (samplesCount == 0)
            return 0;

        double r = 0;
        unsafe
        {
            var samples = stackalloc ProfilerRecorderSample[samplesCount];
            recorder.CopyTo(samples, samplesCount);
            for (var i = 0; i < samplesCount; ++i)
                r += samples[i].Value;
            r /= samplesCount;
        }

        return r;
    }

    void OnEnable()
    {
        systemMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
        gcMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
        mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", 15);
    }

    void OnDisable()
    {
        systemMemoryRecorder.Dispose();
        gcMemoryRecorder.Dispose();
        mainThreadTimeRecorder.Dispose();
    }

    void Update()
    {
        var sb = new StringBuilder(500);
        sb.AppendLine($"Frame Time: {GetRecorderFrameAverage(mainThreadTimeRecorder) * (1e-6f):F1} ms");
        sb.AppendLine($"GC Memory: {gcMemoryRecorder.LastValue / (1024 * 1024)} MB");
        sb.AppendLine($"System Memory: {systemMemoryRecorder.LastValue / (1024 * 1024)} MB");
        statsText = sb.ToString();
    }

    void OnGUI()
    {
        GUI.TextArea(new Rect(10, 30, 250, 50), statsText);
    }
}
```

**Note:**

ProfilerRecorder allocates unmanaged resources and implements IDisposable interface. Use [ProfilerRecorder.Dispose](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/dispose.md) to free resources when you no longer need to record statistics.

ProfilerRecorder gives you access to Unity metrics in two modes: immediate access to a value of a counter, and the counter value when the frame ends. Additional Resources: [ProfilerRecorder.CurrentValue](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/currentvalue.md), [ProfilerRecorder.LastValue](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/lastvalue.md), [ProfilerRecorder.GetSample](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/getsample.md), [ProfilerRecorderHandle.GetAvailable](/engine/6000.5/script-reference/unity/profiling/lowlevel/unsafe/profilerrecorderhandle/getavailable.md).

## Constructors

| Constructor                                                                                                                                                                                                                                                                               | Description                                                                                       |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [ProfilerRecorder(System.String,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(string-int-profilerrecorderoptions\))                                                                   | Constructs ProfilerRecorder instance with a Profiler metric name.                                 |
| [ProfilerRecorder(System.String,System.String,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(string-string-int-profilerrecorderoptions\))                                              | Constructs ProfilerRecorder instance with a Profiler metric name and category.                    |
| [ProfilerRecorder(Unity.Profiling.LowLevel.Unsafe.ProfilerRecorderHandle,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(profilerrecorderhandle-int-profilerrecorderoptions\))          | Constructs ProfilerRecorder instance with a Profiler metric name pointer or other unsafe handles. |
| [ProfilerRecorder(Unity.Profiling.ProfilerCategory,System.Char\*,System.Int32,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(profilercategory-char*-int-int-profilerrecorderoptions\)) | Constructs ProfilerRecorder instance with a Profiler metric name pointer or other unsafe handles. |
| [ProfilerRecorder(Unity.Profiling.ProfilerCategory,System.String,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(profilercategory-string-int-profilerrecorderoptions\))                 | Constructs ProfilerRecorder instance with a Profiler metric name and category.                    |
| [ProfilerRecorder(Unity.Profiling.ProfilerMarker,System.Int32,Unity.Profiling.ProfilerRecorderOptions)](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/ctor.md#profilerrecorder\(profilermarker-int-profilerrecorderoptions\))                                          | Constructs ProfilerRecorder instance with a Profiler metric name pointer or other unsafe handles. |

## Properties

| Property                                                                                                         | Description                                                                               |
| ---------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [Capacity](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/capacity.md)                         | Maximum amount of samples ProfilerRecorder can capture.                                   |
| [Count](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/count.md)                               | Collected samples count.                                                                  |
| [CurrentValue](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/currentvalue.md)                 | Gets current value of the Profiler metric.                                                |
| [CurrentValueAsDouble](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/currentvalueasdouble.md) | Gets current value of the Profiler metric as *double* value.                              |
| [DataType](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/datatype.md)                         | Value data type of the Profiler metric.                                                   |
| [IsRunning](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/isrunning.md)                       | Indicates if ProfilerRecorder is attached to the Profiler metric.                         |
| [LastValue](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/lastvalue.md)                       | Gets the last value collected by the ProfilerRecorder.                                    |
| [LastValueAsDouble](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/lastvalueasdouble.md)       | Gets the last value collected by the ProfilerRecorder as *double*.                        |
| [UnitType](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/unittype.md)                         | Unit type.                                                                                |
| [Valid](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/valid.md)                               | Indicates whether ProfilerRecorder is associated with a valid Profiler marker or counter. |
| [WrappedAround](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/wrappedaround.md)               | Indicates if ProfilerRecorder capacity has been exceeded.                                 |

## Methods

| Method                                                                                     | Description                                           |
| ------------------------------------------------------------------------------------------ | ----------------------------------------------------- |
| [CopyTo](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/copyto.md)       | Copies all collected samples to the destination list. |
| [Dispose](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/dispose.md)     | Releases unmanaged instance of the ProfilerRecorder.  |
| [GetSample](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/getsample.md) | Gets sample data.                                     |
| [Reset](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/reset.md)         | Stops data collection and clears collected samples.   |
| [Start](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/start.md)         | Start data collection.                                |
| [Stop](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/stop.md)           | Stops data collection.                                |
| [ToArray](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/toarray.md)     | Use to convert collected samples to an array.         |

## Static Methods

| Method                                                                                   | Description                                                              |
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| [StartNew](/engine/6000.5/script-reference/unity/profiling/profilerrecorder/startnew.md) | Initialize a new instance of ProfilerRecorder and start data collection. |
