I think that some of the internal actions make this question fair. I do this because of the back of my head, as I ran into this problem a while ago and it took me a day to track, including the extensive use of reflector and memory profiling ANTS (in my previous company) ... here goes:
What the internal XML serializer does, it creates a class (let it be called "A") using System.Reflection.Emit, which takes on the type you pass to it. Building such a class costs a lot of time relatively conditionally and can be reused, because the types do not change. Because of this, constructed types are stored in a dictionary, for example. it ends with some Dictionary.
For known (basic) types, the serializer code is fixed, for example. serialization of the string will not change no matter how many times you restart the application. Note the difference with “A,” where any type that is not known to serialize the factory until it is passed first to XMLSerializer.
The first time a type is used by XMLSerializer, this process is performed both for the type you are passing and for all types that it needs (for example, for all fields and properties that require serialization).
About the leak ... When you call ChannelFactory, it builds a serializer, if it does not already exist. To do this, it checks if the serializer exists in the dictionary, and if not, one instance of the ISomeSerializerType type is created.
For some stupid reason, there is an error in the factory that builds a new serializer without saving it in the dictionary. Once created, you get a new type - which appears as a leak (remember: types can never be unloaded) - even if the objects are correctly positioned. When you first use the XMLSerializer or create a static class, it correctly uses the dictionary cache, which means that it will not leak. So you have it, this is a mistake . I used to have access to the ANTS Memory Profiler, which showed it pretty well.
Hope this explains.
atlaste
source share