perfview icon indicating copy to clipboard operation
perfview copied to clipboard

GenFragmentationPercent(Gen) sometimes throws a null ref exception

Open mrsharm opened this issue 1 year ago • 0 comments

The call to GenSizeAfterMB in the GenFragmentationPercent call is what causes the null ref.

    public double GenFragmentationPercent(Gens gen)
    {
        return GenFragmentationMB(gen) * 100.0 / GenSizeAfterMB(gen);
    }

That GenSizeAfterMB call fails if the HeapStats are somehow null:

    public double GenSizeAfterMB(Gens gen)
    {
        return gen switch
        {
            Gens.GenPinObj => (double)HeapStats.GenerationSize4 / 1000000.0,
            Gens.GenLargeObj => (double)HeapStats.GenerationSize3 / 1000000.0,
            Gens.Gen2 => (double)HeapStats.GenerationSize2 / 1000000.0,
            Gens.Gen1 => (double)HeapStats.GenerationSize1 / 1000000.0,
            Gens.Gen0 => (double)HeapStats.GenerationSize0 / 1000000.0,
            _ => double.NaN,
        };
    }

This should be guarded against.

mrsharm avatar Mar 21 '24 18:03 mrsharm