A
A
Alexander Vishnyakov2013-12-01 08:22:10
.NET
Alexander Vishnyakov, 2013-12-01 08:22:10

Strange .NET Framework version-specific garbage collection issue?

There is an interesting unit test bug in my project that only shows up in .NET 4.5, not .NET 4.0 . Here is the source code, and here is the archive with binaries.
Steps to reproduce the problem:
1. Download the source code and binaries
2. Copy the Binary folder to the Interop\Main\ folder
3. Run the unit tests from the Interop.Core.Tests.NETFX45 assembly The IsAliveAfterCollectionWork test
should fail. If you try looking at the memory with a profiler (I'm using the free dotMemory 4.0 EAPwith the Profile .NET process -> Collection every N-th object: 1, Memory traffic: Collect and Use profiler API settings), you will see that the MarkedObject[] array is not collected by the garbage collector, although there are no references to it .
My configuration:
- Windows 8.1
- .NET Framework 4.5.1
- I'm using Visual Studio 2012 but 2010 and 2013 are installed too

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mayorovp, 2013-12-01
@mayorovp

What's strange here? GC does not owe you anything if the object has passed to the next generation - Collect can ignore it.
Try GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);

A
andreycha, 2013-12-28
@andreycha

In debug mode, local variables are kept alive until the end of the current scope. This is done in order for the Locals, Auto functions to work, i.e. so that you can see the states of local variables at any time during debugging. I rather have a question why your test does not fall in 4.0.

I
Ilya Glebov, 2013-12-06
@IljaGlebov

Compiled VS 13 from source.
If you run Resharper's test runner, the test passes
If you run Resharper's test runner in debug, the test crashes
If you run VS test runner, the test crashes
If you make a console application, then 1 object remains in WeakReferencesStorage
It might be worth digging towards the work of the GC in the debug environment

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question