.net - Memory leak wcf with datacontract resolver -
i've done simple test of wcf service - call 1 method. , profile memory usage.
memory usage grows. bu why?
main memory occupiers market above.
update
i can't post commercial code , code large. i've found 1 interesting thing. if method call emits call of data contract resolver memory usage constantrly grows. if method call not emit call of datacontract resolver memory usage not grow.
my datacontract resolver looks 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 difference:
1.before
2.after
not xmldictionarystring's not int32[]'s
to avoid memory leak don't use
xmldictionary dictionary = new xmldictionary();
in every resolve call
Comments
Post a Comment