Skip to content

Memory leak and perf getting worse for TryGet with missing bindings #398

@Julie-BC

Description

@Julie-BC

Hi NInject Team,

We have detected a memory leak with NInject 3.3.6
This would have been solved with last changes for 4.0.0-beta but I think important to report it.

It's depending on the context but it is easily reproducible.
Just make sure about both conditions :

  • The element was not previously registered
  • The resolve is tried about a concrete type (not an interface)
using System;
using System.Diagnostics;
using System.Threading;
using Ninject;

namespace NInjectMemoryLeak;

/// <summary>
/// Program
/// </summary>
public static class Program
{
  public static void Main()
  {
    using var container = new StandardKernel();

    for (var i = 0; i < int.MaxValue; i++)
    {
      var watch = Stopwatch.StartNew();
      container.TryGet<object>("toto");
      var duration = watch.Elapsed;

      Console.WriteLine($"{i} => {duration}");

      if (i % 1000 == 0)
      {
        GC.Collect();
        Thread.Sleep(1000);
      }
    }

    Console.Read();
  }
}

You will have good perf at first
image
but a memory leak occurs and performance decreases
image
[...]
image

And this seems to be because of missing bindings adds
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions