GC.GetTotalMemory方法

GC.GetTotalMemory方法
方法描述:
    获取目前所有受控代码分配的字节数。带有一个参数,bool值,用来标识是否先进行垃圾收集再计算字节数。
命名空间:System
Assembly:mscolib.dll
方法签名:
    public static long GetTotalMemory(bool forceFullCollection)
参数说明:
    forceFullCollection
    类型:System.Boolean
    true表示该方法先做垃圾收集操作,之后才计算出受控代码分配的字节数。false表示不做垃圾收集,直接计算出受控代码分配的字节数。
返回值:
    类型:System.Int64
    返回目前所有受控代码所分配的最大可能的一个近似值。
注意:
    如果参数forceFullCollection为true的话,该方法内部先做垃圾收集,时间的长短跟垃圾的多少有关系,并且该垃圾收集不保证所有不可访问的对象都能够被回收。
例子:
  1. using System;
  2. namespace GCCollectIntExample
  3. {
  4.     class MyGCCollectClass
  5.     {
  6.         private const long maxGarbage = 1000;
  7.         static void Main()
  8.         {
  9.             MyGCCollectClass myGCCol = new MyGCCollectClass();
  10.             // Determine the maximum number of generations the system
  11.             // garbage collector currently supports.
  12.             Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);
  13.             myGCCol.MakeSomeGarbage();
  14.             // Determine which generation myGCCol object is stored in.
  15.             Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
  16.             // Determine the best available approximation of the number 
  17.             // of bytes currently allocated in managed memory.
  18.             Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
  19.             // Perform a collection of generation 0 only.
  20.             GC.Collect(0);
  21.             // Determine which generation myGCCol object is stored in.
  22.             Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
  23.             Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
  24.             // Perform a collection of all generations up to and including 2.
  25.             GC.Collect(2);
  26.             // Determine which generation myGCCol object is stored in.
  27.             Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
  28.             Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
  29.             Console.Read();
  30.         }
  31.         void MakeSomeGarbage()
  32.         {
  33.             Version vt;
  34.             for (int i = 0; i < maxGarbage; i++)
  35.             {
  36.                 // Create objects and release them to fill up memory
  37.                 // with unused objects.
  38.                 vt = new Version();
  39.             }
  40.         }
  41.     }
  42. }
输出结果:
the highest generation is 2
Generation:0
Total Memory:519676
Generation:1
Total Memory:196004
Generation:2
Total Memory:195764   
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值