【发布时间】:2015-07-14 13:29:49
【问题描述】:
我对我的 wcf 服务进行了简单的测试 - 不断调用一种方法。然后我分析它的内存使用情况。
内存使用量不断增长。但为什么?
主要的内存占用者在市场之上。
更新
无法发布商业代码,代码太大。但是我发现了一件有趣的事情。如果我的方法调用发出数据合约解析器的调用,那么内存使用量就会不断增长。如果方法调用没有发出数据合约解析器的调用,则内存使用量不会增长。
我的数据合约解析器如下所示:
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);
}
}
...
}
怎么了?
更新 2
我已经做了简单的解决方法:
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;
}
...
}
...
}
看看区别:
1.之前
2.之后
不是 XmlDictionaryString 不是 int32[] 的
【问题讨论】:
-
您需要发布调用违规函数的代码。
标签: .net wcf memory-leaks