ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

C#反射之使用绑定句柄减少进程的内存消耗

2020-05-11 19:53:07  阅读:356  来源: 互联网

标签:methodHandles Show C# 句柄 绑定 BindingFlags GC methodInfoList MethodInfo


最近在看反射方面的东西的时候,看到最后发现一个与内存优化相关的东西,在此记录一下:

以下是文中介绍的 MethodInfo 和 RuntimeMethodHandle 相互转换测试代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Reflection;
 5 
 6 namespace GenDLL
 7 {
 8     class Class1
 9     {
10         const BindingFlags bFlags = BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
11         public static void Main(string[] args)
12         {
13             //显示在任何反射操作之前的堆栈的大小
14             Show("Before doing anying");
15 
16             //为MSCorlib.dll中的所有方法构建 MethodInfo 对象缓存
17             List<MethodBase> methodInfoList = new List<MethodBase>();
18             //遍历定义  Object 类的程序集中定义的公共类型
19             foreach (Type type in typeof(Object).Assembly.GetExportedTypes())
20             {
21                 //过滤任何泛型的类型
22                 if (type.IsGenericTypeDefinition) continue;
23                 MethodBase[] array = type.GetMethods(bFlags);
24                 methodInfoList.AddRange(array);
25             }
26 
27             //显示当绑定所有方法之后,方法的个数和堆的大小
28             Console.WriteLine("# of method = {0:N0} ", methodInfoList.Count);
29             Show("After building cache of MethodInfo Object");
30 
31             //为所有的 MethodInfo 对象构建 RuntimeMethodHandle缓存
32             List<RuntimeMethodHandle> methodHandles = methodInfoList.ConvertAll<RuntimeMethodHandle>(mb => mb.MethodHandle);
33             Show("Holding MethodInfo and RuntimeMethodHandle of cache");
34             GC.KeepAlive(methodInfoList);   //阻止缓存被过早的回收
35             methodInfoList = null;          //现在允许进行回收
36             Show("After free MethodInfo Objects");
37 
38             methodInfoList = methodHandles.ConvertAll<MethodBase>(rmh => MethodBase.GetMethodFromHandle(rmh));                  //方式一
39             // methodInfoList = methodHandles.ConvertAll<MethodBase>(rmh => { return MethodBase.GetMethodFromHandle(rmh); });   //方式二
40             Show("Size of heap  After re-creating MethodInfo Objects");
41 
42             GC.KeepAlive(methodInfoList);   //阻止缓存被过早的回收
43             GC.KeepAlive(methodHandles);    //阻止缓存被过早的回收
44 
45             methodInfoList = null;
46             methodHandles = null;
47 
48             Show("After freeing  MethodInfos and RuntimeMethodHandles");
49 
50             Console.ReadLine();
51         }
52 
53         static void Show(string str)
54         {
55             //用逗号隔开的数字,0 表示占位符   12:N0 表示用逗号分隔数字,至少占12个字符,小数点后的位数为0
56             string format = "Heap Size = {0,12:N0} - {1}";
57             Console.WriteLine(format, GC.GetTotalMemory(true), str);
58         }
59     }
60 }

 

编程小知识之 GC.KeepAlive :https://blog.csdn.net/tkokof1/article/details/92073033

 

标签:methodHandles,Show,C#,句柄,绑定,BindingFlags,GC,methodInfoList,MethodInfo
来源: https://www.cnblogs.com/luguoshuai/p/12871330.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有