.NET(C#):获取进程的内存私有工作集
2020-12-13 02:35
标签:style blog class code java tar 将读取的数据写到log.txt文件中,先在工程下新建一个log.txt文件 MSDN: http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.pagedmemorysize64.aspx 来源:“任务管理器”内存列的含义是什么? http://windows.microsoft.com/zh-CN/windows7/What-do-the-Task-Manager-memory-columns-mean .NET(C#):获取进程的内存私有工作集,搜素材,soscw.com .NET(C#):获取进程的内存私有工作集 标签:style blog class code java tar 原文地址:http://www.cnblogs.com/kennyliu/p/3718790.htmlpublic 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);
}
}
}