C# 使用绑定句柄来减少进程的内存耗用

许多应用程序中,绑定了一组类型(type)或者类型成员(从memberinfo派生),并将这些对象保存在某种形式的一个集合中。以后,会搜索这个集合,查找特定的对象,然后调用这个对象。这是一个很好的机制,但是有个小问题:type和memberinfo派生的对象需要大量的内存。如果一个应用程序容纳了太多这样的类,但只是偶尔用一下它们,应用程序的内存就会急剧增长,对应用程序的性能产生影响。

在内部,CLR用一种更精简的形式来表示这种信息。CLR之所以为应用程序创建这些对象,只是为了简化开发人员的工作。CLR在运行时并不需要这些大对象。如果需要缓存大量Type和MemberInfo派生对象,开发人员可以使用运行时句柄(runtime handle)来代替对象,从而减少工作集(占用的内存)。FCL定义了3个运行时句柄类型(都在System命名空间中),RuntimeTypeHandle,RuntimeFieldHandle,RumtimeMethodHandle。三个类型都是值类型,他们只包含了一个字段,也就是一个IntPtr;这样一来,这些类型的实例就相当省内存。ItPtr字段是一个句柄,它引用了AppDomain的Loader堆中的一个类型,字段或方法。转换方法:

Type→RuntimeTypeHandle,通过查询Type的只读字段属性TypeHandle。

RuntimeTypeHandle→Type,通过调用Type的静态方法GetTypeFromHanlde。

FieldInfo→RuntimeFieldHandle,通过查询FieldInfo的实例只读字段FieldHandle。

RuntimeFieldHandle→FieldInfo,通过调用FieldInfo的静态方法GetFieldFromHandle。

MethodInfo→RuntimeMethodHandle,通过查询MethodInof的实例只读字段MethodHandle。

RuntimeMethodHandle→MethodInfo,通过调用MethodInfo的静态方法GetMethodFromHandle。

下面的示例获取许多的MethodInfo对象,把它们转化成RuntimeMethodHandle实例,并演示转换前后的内存差异。

 private void UseRuntimeHandleToReduceMemory()        {            Show("Before doing anything");//从MSCorlib.dll中地所有方法构建methodInfos 对象缓存            List methodInfos = new List();                        foreach (Type t in typeof(object).Assembly.GetExportedTypes())            {                if (t.IsGenericType) continue;                MethodBase[] mbs = t.GetMethods(c_bf);                methodInfos.AddRange(mbs);            }            //显示当绑定所有方法之后,方法的个数和堆的大小            Console.WriteLine("# of Methods={0:###,###}", methodInfos.Count);            Show("After building cache of MethodInfo objects");//为所有MethodInfo对象构建RuntimeMethodHandle缓存            List methodHandles = new List();            methodHandles = methodInfos.ConvertAll(m => m.MethodHandle);            Show("Holding MethodInfo and RuntimeMethodHandle");            GC.KeepAlive(methodHandles);//阻止缓存被过早垃圾回收            methodInfos = null;//现在允许缓存垃圾回收            Show("After freeing MethodInfo objects");            methodInfos = methodHandles.ConvertAll(r => MethodBase.GetMethodFromHandle(r));            Show("Size of heap after re-creating methodinfo objects");            GC.KeepAlive(methodHandles);//阻止缓存被过早垃圾回收            GC.KeepAlive(methodInfos);//阻止缓存被过早垃圾回收            methodInfos = null;//现在允许缓存垃圾回收            methodHandles = null;//现在允许缓存垃圾回收            Show("after freeing MethodInfo and MethodHandle objects");        }

结果如下:

Heap Size =     114,788 - Before doing anything# of Methods=10,003Heap Size =   2,205,652 - After building cache of MethodInfo objectsHeap Size =   2,245,744 - Holding MethodInfo and RuntimeMethodHandleHeap Size =   2,171,976 - After freeing MethodInfo objectsHeap Size =   2,327,516 - Size of heap after re-creating methodinfo objectsHeap Size =     247,028 - after freeing MethodInfo and MethodHandle objects

本文整理自《NET CLR via C#》

作者:jiankunking 出处:http://www.php.cn/

许多应用程序中,绑定了一组类型(type)或者类型成员(从memberinfo派生),并将这些对象保存在某种形式的一个集合中。以后,会搜索这个集合,查找特定的对象,然后调用这个对象。这是一个很好的机制,但是有个小问题:type和memberinfo派生的对象需要大量的内存。如果一个应用程序容纳了太多这样的类,但只是偶尔用一下它们,应用程序的内存就会急剧增长,对应用程序的性能产生影响。

在内部,CLR用一种更精简的形式来表示这种信息。CLR之所以为应用程序创建这些对象,只是为了简化开发人员的工作。CLR在运行时并不需要这些大对象。如果需要缓存大量Type和MemberInfo派生对象,开发人员可以使用运行时句柄(runtime handle)来代替对象,从而减少工作集(占用的内存)。FCL定义了3个运行时句柄类型(都在System命名空间中),RuntimeTypeHandle,RuntimeFieldHandle,RumtimeMethodHandle。三个类型都是值类型,他们只包含了一个字段,也就是一个IntPtr;这样一来,这些类型的实例就相当省内存。ItPtr字段是一个句柄,它引用了AppDomain的Loader堆中的一个类型,字段或方法。转换方法:

Type→RuntimeTypeHandle,通过查询Type的只读字段属性TypeHandle。

RuntimeTypeHandle→Type,通过调用Type的静态方法GetTypeFromHanlde。

FieldInfo→RuntimeFieldHandle,通过查询FieldInfo的实例只读字段FieldHandle。

RuntimeFieldHandle→FieldInfo,通过调用FieldInfo的静态方法GetFieldFromHandle。

MethodInfo→RuntimeMethodHandle,通过查询MethodInof的实例只读字段MethodHandle。

RuntimeMethodHandle→MethodInfo,通过调用MethodInfo的静态方法GetMethodFromHandle。

下面的示例获取许多的MethodInfo对象,把它们转化成RuntimeMethodHandle实例,并演示转换前后的内存差异。

 private void UseRuntimeHandleToReduceMemory()        {            Show("Before doing anything");//从MSCorlib.dll中地所有方法构建methodInfos 对象缓存            List methodInfos = new List();                        foreach (Type t in typeof(object).Assembly.GetExportedTypes())            {                if (t.IsGenericType) continue;                MethodBase[] mbs = t.GetMethods(c_bf);                methodInfos.AddRange(mbs);            }            //显示当绑定所有方法之后,方法的个数和堆的大小            Console.WriteLine("# of Methods={0:###,###}", methodInfos.Count);            Show("After building cache of MethodInfo objects");//为所有MethodInfo对象构建RuntimeMethodHandle缓存            List methodHandles = new List();            methodHandles = methodInfos.ConvertAll(m => m.MethodHandle);            Show("Holding MethodInfo and RuntimeMethodHandle");            GC.KeepAlive(methodHandles);//阻止缓存被过早垃圾回收            methodInfos = null;//现在允许缓存垃圾回收            Show("After freeing MethodInfo objects");            methodInfos = methodHandles.ConvertAll(r => MethodBase.GetMethodFromHandle(r));            Show("Size of heap after re-creating methodinfo objects");            GC.KeepAlive(methodHandles);//阻止缓存被过早垃圾回收            GC.KeepAlive(methodInfos);//阻止缓存被过早垃圾回收            methodInfos = null;//现在允许缓存垃圾回收            methodHandles = null;//现在允许缓存垃圾回收            Show("after freeing MethodInfo and MethodHandle objects");        }

结果如下:

Heap Size =     114,788 - Before doing anything# of Methods=10,003Heap Size =   2,205,652 - After building cache of MethodInfo objectsHeap Size =   2,245,744 - Holding MethodInfo and RuntimeMethodHandleHeap Size =   2,171,976 - After freeing MethodInfo objectsHeap Size =   2,327,516 - Size of heap after re-creating methodinfo objectsHeap Size =     247,028 - after freeing MethodInfo and MethodHandle objects

以上就是C# 使用绑定句柄来减少进程的内存耗用的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 06:23:45
下一篇 2025年12月17日 06:23:55

相关推荐

发表回复

登录后才能评论
关注微信