Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
GC does not clean up memory allocated in same method #20156
This only happens in debug builds, when using release builds, the code below works properly.
I'm aware of the release / debug GC behaviors with respect to not eagerly clearing out variables that are still in scope, but in this case, we are explicitly setting the value to null and invoking the GC to clean things up.
This actually showed up for us when we noticed high memory utilization in a scenario where it shouldn't. This was using a debug build, and while we cleared up the field that was holding the value, we had the same experience. Under debug, there is something else that holds a reference to the value.
This is same issue as:
The JIT can produce temporary local variables. These temporary local variables are outside your control and they may result into the reference lifetime getting extended. The likelihood of this happening is much higher with optimizations off. This issue can happen with optimizations on as well for more complex methods.
A slightly adjusted version is here:
This simulates the original issue we had, a long running loop that holds state and clears it.
It doesn't reproduce in release, but I'm worried if something like this can happen on release?
Also, WinDBG gives inconsistent results:
I wonder if a good choice for long running loops would be to do the inner loop work in its own method, to avoid this ?