FunctorPrototype
FunctorPrototype

Reputation: 1203

Memory leak wcf with datacontract resolver

I've done simple test of my wcf service - constantly call one method. And then I profile it memory usage.

enter image description here

Memory usage constantly grows. But why?

enter image description here

Main memory occupiers are market above.

UPDATE

I can't post commercial code and the code is too large. But I've found one interesting thing. If my method call emits call of data contract resolver then memory usage constantrly grows. If method call does not emit call of datacontract resolver then memory usage does not grow.

My datacontract resolver looks like this:

public class MyResolver : DataContractResolver
{
    public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, 
        out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {

        if (dataContractType == typeof(MyDerivedType))
        {
            XmlDictionary dictionary = new XmlDictionary();
            typeName = dictionary.Add("MyDerivedType");
            typeNamespace = dictionary.Add("http://tempuri.com");
            return true;
        }
        else
        {
            return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
        }
    }
    ...
}

What's wrong?

UPDATE 2

I've done simple workaround:

 public class MyResolver : DataContractResolver
{
    private static  Dictionary<string,XmlDictionary> _typesCache=new Dictionary<string, XmlDictionary>();
    static MyResolver()
    {
         XmlDictionary myDerivedTypeDictionary = new XmlDictionary();
         myDerivedTypeDictionary.Add("MyDerivedType");
         myDerivedTypeDictionary.Add("http://tempuri.com");
         _typesCache["MyDerivedType"] = myDerivedTypeDictionary ;
    }
     public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, 
        out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {
       if (dataContractType == typeof(MyDerivedType))
        {
            XmlDictionary dictionary = _typesCache["MyDerivedType"];
            XmlDictionaryString typeNameDictionaryString;
            dictionary.TryLookup("MyDerivedType", out typeNameDictionaryString);
            XmlDictionaryString namespaceDictionaryString;
            dictionary.TryLookup("http://tempuri.com", out namespaceDictionaryString);
            typeName = typeNameDictionaryString;
            typeNamespace = namespaceDictionaryString;

            return true;
        }
        ...
     }
     ...
}

and see the difference:

1.before

enter image description here

2.after

enter image description here

Not XmlDictionaryString's not int32[]'s

Upvotes: 4

Views: 1053

Answers (1)

FunctorPrototype
FunctorPrototype

Reputation: 1203

To avoid memory leak don't use

XmlDictionary dictionary = new XmlDictionary();

in every resolve call

Upvotes: 3

Related Questions