日期:2014-05-18  浏览次数:20930 次

c# 里面怎么实时的真测一个程序的内存消耗
如题,最好能给些代码。

------解决方案--------------------
调用性能计数器
C# code

class Program
    {
        //实例化一个性能计数器,统计可用内存数(消耗的话就用总内存数去减去可用数)
        private static System.Diagnostics.PerformanceCounter perfCounter= new System.Diagnostics.PerformanceCounter("Memory", "Available KBytes");
        static void Main(string[] args)
        {
            System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(counter),null,0,1000);
            
            Console.ReadLine();
        }

        private static void counter(object state)
        {
            float count = perfCounter.NextValue();
            Console.WriteLine(count + "KBytes");
        }
    }