.NET(C#):获取进程的内存私有工作集(获得每个程序占用的内存)
2017-08-05 19:45:04 访问(4044) 赞(0) 踩(0)
相关下载:SlowX.ExamMemory(执行程序) SlowX.ExamMemory(源码)
比如输出:
3348
工作集(进程类)9,588.00 KB
工作集 9,588.00 KB
私有工作集 3,672.00 KB
2017-08-05 19:49:38 ;内存(专用工作集)3,672.00;PID:3348;程序名:VisualSVNServer
对应的任务管理器显示也是该值
using System;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace SlowX.ExamMemory
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void toolStripButton_获得内存信息_Click(object sender, EventArgs e)
{
try
{
string processName = null;
Process[] ps = Process.GetProcesses();
StringBuilder theResult = new StringBuilder();
foreach (Process p in ps)
{
if (p.MainWindowHandle != null)
{
processName = p.ProcessName;
getWorkingSet(theResult, processName);
theResult.AppendLine();
}
}
richTextBox1.Text = theResult.ToString();
}
catch(Exception err)
{
richTextBox1.Text = "异常:" + err.Message;
}
}
public void getWorkingSet(StringBuilder theResult, string processName)
{
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))
{
theResult.AppendLine(process.Id.ToString());
//注意除以CPU数量
theResult.AppendLine(string.Format("{0}{1:N} KB", "工作集(进程类)", process.WorkingSet64 / 1024));
theResult.AppendLine(string.Format("{0}{1:N} KB", "工作集 ", process.WorkingSet64 / 1024));
theResult.AppendLine(string.Format("{0}{1:N} KB", "私有工作集 ", p1.NextValue() / 1024));
theResult.AppendLine(string.Format("{0};内存(专用工作集){1:N};PID:{2};程序名:{3}", DateTime.Now, p1.NextValue() / 1024, process.Id.ToString(), processName));
}
}
}
}
}
}
上一条:
下一条:
相关评论
发表评论