Counting allocations in Unity
May 4, 2023
Have you ever wondered how many heap allocations your C# code is making? Here’s a little class called AllocCounter that will help you do that. It’s pretty straightforward:
var ac = new AllocCounter();
// code that may or may not do allocations
var allocCount = ac.Stop();
If you like lambdas, you can use this style as well:
var allocCount = AllocCounter.Instrument(() => {
// code that may or may not allocate
});