.NET(C#):获取进程的内存私有工作集

时间:2014-05-09 23:03:52   收藏:0   阅读:843

将读取的数据写到log.txt文件中,先在工程下新建一个log.txt文件

bubuko.com,布布扣
public class Program
    {
        public const string LOGGER_FILE = "log.txt";
        public static void Logger(string formatString, params object[] args)
        {
            string input = string.Format(formatString, args);
            System.IO.File.AppendAllText(LOGGER_FILE, string.Concat(input, "\r\n"));
        }
        static void Main(string[] args)
        {
            
            string WindowTitle = string.Empty;
            long MemorySize;
            long PagedMemorySize;
            long PeakWorking;
            string ProcessName;
            int pid;
            Process[] ps = Process.GetProcesses();
            foreach (Process p in ps)
            {
                  if (p.MainWindowHandle != null)
                {
                    ProcessName = p.ProcessName;
                    getWorkingSet(ProcessName);


                    //pid = p.Id;
                    //MemorySize = p.NonpagedSystemMemorySize64;
                    ////MachineName =  p.MachineName;
                    //PagedMemorySize = p.PagedMemorySize64;
                    //PeakWorking = p.PeakWorkingSet64;
                    //WindowTitle = p.MainWindowTitle;                   

                    //Logger("时间{0}:MemorySize{1};PagedMemorySize{2};PeakWorking{3};{4};PID:{5}", DateTime.Now, MemorySize.ToString(), PagedMemorySize.ToString(), PeakWorking.ToString(), WindowTitle,pid);

                }
            }
            Console.WriteLine("已完成请查看log.txt,输入任意键退出。");
            Console.ReadKey();



        }

        public static void getWorkingSet(string processName)
        {
            //var processName = "taskmgr";
            using (var process = Process.GetProcessesByName(processName)[0])
            using (var p1 = new PerformanceCounter("Process", "Working Set - Private", processName))
            using (var p2 = new PerformanceCounter("Process", "Working Set", processName))
            {
                Console.WriteLine(process.Id);
                //注意除以CPU数量
                Console.WriteLine("{0}{1:N} KB", "工作集(进程类)", process.WorkingSet64 / 1024);
                Console.WriteLine("{0}{1:N} KB", "工作集 ", process.WorkingSet64 / 1024);
                Console.WriteLine("{0}{1:N} KB", "私有工作集 ", p1.NextValue() / 1024);
                Logger("{0};内存(专用工作集){1:N};PID:{2};程序名:{3}", DateTime.Now, p1.NextValue() / 1024, process.Id.ToString(), processName);

               // Thread.Sleep(1000);
            }
        }
    }
bubuko.com,布布扣

MSDN: http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.pagedmemorysize64.aspx

 

来源:“任务管理器”内存列的含义是什么? windows.microsoft.com/z

.NET(C#):获取进程的内存私有工作集,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!