什么是GC.GetTotalMemory(假)和GC.GetTotalMemory之间的差(真) [英] What is the Difference between GC.GetTotalMemory(false) and GC.GetTotalMemory(true)

查看:1685
本文介绍了什么是GC.GetTotalMemory(假)和GC.GetTotalMemory之间的差(真)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人告诉我GC.GetTotalMemory(假)之间的差异, GC.GetTotalMemory(真);

我有一个小程序,当我比较结果的第一环 给出了一个放放<循环计数0的Diff = 32>为GC.GetTotalMemory(真正的); 和<循环计数0的Diff = 0> GC.GetTotalMemory(假); 但不应该是它的otherway?

环的Smilarly其余打印一些数字,这是两种不同的情况。这是什么数表示。为什么是它改变为循环增加。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Diagnostics程序;

命名空间测试
{
   结构地址
   {
       公共字符串街;
   }
   一流的详细信息
   {
      公共字符串名称;
      公共广播地址=新的地址();

   }
   类EMP:IDisposable的
   {
       公众详细objb =新的细节();
       布尔处置= FALSE;
       #地区IDisposable的成员
       公共无效的Dispose()
       {
           处置(真);
       }
       无效的销毁(BOOL处置)
       {
           如果(!处置)
               处置=处置;
           objb = NULL;
           GC.Sup pressFinalize(本);
       }

       #endregion
   }


    类节目
    {
        静态无效的主要(字串[] args)
        {
           长尺寸1 = GC.GetTotalMemory(假);
           EMP empobj = NULL;
           的for(int i = 0; I< 200;我++)
           {
              //使用(empobj =新EMP())// -------(1)
               {
                   empobj =新EMP(); // -------(2)
                   empobj.objb.Name =ssssssssssssssssss;
                   empobj.objb.address.Street =asdfasdfasdfasdf;
               }

              长size2个= GC.GetTotalMemory(假);
              Console.WriteLine(循环计数+ 1 +的Diff =+(size2个-尺寸1));

           }
        }

    }
}
 

解决方案

参数定义是否要等到一个的完全的垃圾收集运行,或者在此之前发生的。

请参阅 MSDN : 参数

forceFullCollection
类型:System.Boolean
真要表明,该方法可以等待垃圾回收返回前发生;否则为false。

的原因,差异仍然是0可能是因为GC已happend即使你传递false。

Could some one tell me the difference between GC.GetTotalMemory(false) and GC.GetTotalMemory(true);

I have a small program and when i compared the results the first loop gives an put put < loop count 0 Diff = 32 > for GC.GetTotalMemory(true); and < loop count 0 Diff = 0 > for GC.GetTotalMemory(false); but shouldnt it be the otherway ?

Smilarly rest of the loops prints some numbers ,which are different for both case. what does this number indicate .why is it changing as the loop increase.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace test
{   
   struct Address
   {
       public string Street;
   }
   class Details
   {
      public string Name ;
      public Address address = new Address();

   }
   class emp :IDisposable
   {
       public Details objb = new Details();       
       bool disposed = false;
       #region IDisposable Members
       public void Dispose()
       {
           Disposing(true);
       }
       void Disposing(bool disposing)
       {
           if (!disposed)
               disposed = disposing;
           objb = null;           
           GC.SuppressFinalize(this);
       }

       #endregion
   }


    class Program
    {       
        static void Main(string[] args)
        {        
           long size1 = GC.GetTotalMemory(false);
           emp empobj = null;          
           for (int i = 0; i < 200;i++ )
           {
              // using (empobj = new emp()) //------- (1)
               {
                   empobj = new emp(); //------- (2)
                   empobj.objb.Name = "ssssssssssssssssss";
                   empobj.objb.address.Street = "asdfasdfasdfasdf";
               }

              long size2 = GC.GetTotalMemory(false);             
              Console.WriteLine( "loop count " +i + "  Diff = " +(size2-size1));

           }
        }

    }
}

解决方案

The parameter defines whether or not to wait till a full garbage collection happens before running or not.

See MSDN: Parameters

forceFullCollection
Type: System.Boolean
true to indicate that this method can wait for garbage collection to occur before returning; otherwise, false.

The reason that diff is still 0 could be because a GC already happend even if you pass false.

这篇关于什么是GC.GetTotalMemory(假)和GC.GetTotalMemory之间的差(真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆