Skip to content

MeasureAllocations Method ​

Definition ​

  • Namespace: JASE.Performance
  • Assembly: JASE.dll

long MeasureAllocations(Action action, int iterations = null, bool forceGc = null) ​

csharp
long MeasureAllocations(Action action, int iterations = null, bool forceGc = null)

Measures the average number of bytes allocated by the specified action.

Parameters ​

NameDescription
actionThe delegate to execute while measuring allocations.
forceGcWhen true Garbage Collection is performed prior to Action execution
iterationsNumber of times to execute action. Must be greater than zero.

Returns ​

The average number of bytes allocated per execution of action.

Remarks ​

This method is intended for diagnosing allocation behavior within a controlled code path, not for precise microbenchmarking. It forces a full garbage collection before measurement to reduce noise, captures the thread-local allocation counter before and after execution, and divides the difference by the number of iterations.

Only allocations performed on the current thread are included. If action performs work on other threads or uses async/Task-based operations, the results will not reflect total allocations.