سؤال

I'm struggling with ngen and generic collections. I've ngen'ed all my assemblies in solution, but still somehow jitting is occurred every time my app is executing that code:

private Dictionary<int, bool> VerifyedFunc; 

public SecurityProvider()
{
    ...
    VerifyedFunc = new Dictionary<int, bool>();
    ...       
}

MDA msg:

Managed Debugging Assistant 'JitCompilationStart' has detected a problem in '*.exe'.
Additional Information: 
<mda:msg xmlns:mda="http://schemas.microsoft.com/CLR/2004/10/mda">
<mda:jitCompilationStartMsg break="true">
<method name="mscorlib!System.Collections.Generic.Dictionary`2::.ctor"/>
</mda:jitCompilationStartMsg>
</mda:msg>

Are there are some issues with NGen and generic collections?

هل كانت مفيدة؟

المحلول

Well, this isn't a problem. You made it a problem by using the jitCompilationStartMsg debugging assistant. That simply reports that the jitter got started. We went over this in your previous question about that MDA.

This is otherwise entirely normal and the way that generics work in .NET. The jitter generates the concrete class from the generic cookie-cutter class definition at runtime. There will be one instance of the concrete class for any reference type and one each for every value type that you use in your code.

This is not very compatible with Ngen of course, Dictionary<> is a class in mscorlib.dll and that assembly was ngen-ed when you installed .NET on your machine. Ngen is powerless to guess up front what concrete class types you are going to instantiate in your code. There is a countermeasure for this in mscorlib.dll, it pre-defines a number of generic types so they will be ngen-ed. Like List<int>, very likely to be used in an application. And in the .NET framework itself.

You can see these predeclared generic types from the Reference Source, CommonlyUsedGenericInstantiations() method. Note how it has several precooked versions of Dictionary<> in that method. But not a Dictionary<int, bool>, too unusual. So the jitter is needed to create the type for you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top