问题描述
有谁知道,如果在C#中使用动态的关键字与__ComObject实例时防止RuntimeBinder内存泄漏的方法吗?
Does anybody know if there is a way of preventing a memory leak in RuntimeBinder when using "dynamic" keyword with __ComObject instances in C#?
我得到了下面的代码
var t = Type.GetTypeFromCLSID(new Guid("BB06C0E4-D293-4f75-8A90-CB05B6477EEE"));
while (true)
{
dynamic o = System.Activator.CreateInstance(t);
Marshal.ReleaseComObject(o);
}
这从Microsoft.CSharp.RuntimeBinder泄漏LocalVariableSymbol类实例(和其他。 。语义命名空间)
This leaks LocalVariableSymbol class instances (and other from the Microsoft.CSharp.RuntimeBinder.Semantics namespace).
更换动态与目标,即:
object o = System.Activator.CreateInstance(t);
修复了泄漏,但我更喜欢使用动态,以保持(实际的代码是更加复杂和利用了动态)。
fixes the leak but I'd prefer to keep using dynamic (the actual code is much more complex and makes use of "dynamic").
我知道RuntimeBinder单缓存数据,这将导致泄漏,但你知道,如果有什么办法可以清除缓存等。 ?!
I know the RuntimeBinder singleton caches the data and this causes the leak but do you know if there's any way to cleanup the cache etc.?
非常感谢
类似的问题:
- 的
- Memory Overflow: Having an increasing number of Microsoft.CSharp.RuntimeBinder.Semantics
推荐答案
在我的情况的解决办法是更换:
The solution in my case was to replace:
dynamic o = System.Activator.CreateInstance(t);
与
with:
object o = System.Activator.CreateInstance(t);
dynamic d = o;
内存泄漏不再出现其应用的解决方法。
The memory leak no longer occurs having the workaround applied.
这篇关于动态"使用"时RuntimeBinder泄漏;与__ComObject关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!