Skip to content

GenFragmentationPercent(Gen) sometimes throws a null ref exception #2012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mrsharm opened this issue Mar 21, 2024 · 1 comment · Fixed by #2211
Closed

GenFragmentationPercent(Gen) sometimes throws a null ref exception #2012

mrsharm opened this issue Mar 21, 2024 · 1 comment · Fixed by #2211
Assignees

Comments

@mrsharm
Copy link
Contributor

mrsharm commented Mar 21, 2024

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.

@brianrob
Copy link
Member

If HeapStats is null, then GenSizeAfterMB should return NaN, and NaN should be propagated out of GenFragmentationPercent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants