如何为记事本的所有运行实例查找内存(私有工作集)

本文关键字:内存 查找 工作集 实例 运行 记事本 | 更新日期: 2023-09-27 18:08:44

嗯,我正试图为记事本的所有运行实例获取内存(私有工作集)。例如,假设4个记事本进程正在运行,现在我想要所有4个记事本实例消耗的总内存。

到目前为止,我已经尝试为单个

获取内存(私有工作集)
Process[] proc = Process.GetProcessesByName(Notepad);
foreach (var kl in proc)
{
   idnuml = kl.Id; // fetching pid for Notepad running instance.
}
PerformanceCounter PC = new PerformanceCounter( "Process", "Working Set -  Private", InstanceName);
memsize = Convert.ToInt32(PC.NextValue()) / 1024;

我不知道为什么PerformanceCounter不能与PID一起工作。在每次迭代中,它能够检测不同实例的记事本pid,但在结束时,所有实例的工作集值相同。

请帮帮我。提前致谢

如何为记事本的所有运行实例查找内存(私有工作集)

我知道在这里之前已经回答过了,但只是为了完整的工作代码,我发布了这个解决方案。请注意,此代码基于链接链中M4N提交的方法:

public static long GetProcessPrivateWorkingSet64Size(int process_id)
{
  long process_size = 0;
  Process process = Process.GetProcessById(process_id);
  if (process == null) return process_size;
  string instanceName = GetProcessInstanceName(process.Id);
  var counter = new PerformanceCounter("Process", "Working Set - Private", instanceName, true);
  process_size = Convert.ToInt32(counter.NextValue()) / 1024;
  return process_size;
}
public static string GetProcessInstanceName(int process_id)
{
  PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
  string[] instances = cat.GetInstanceNames();
  foreach (string instance in instances)
  {
    using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
    {
       int val = (int)cnt.RawValue;
       if (val == process_id)
         return instance;
    }
  }
  throw new Exception("Could not find performance counter ");
}

同样,如果您想获得同一进程的多个实例的总内存,请使用上述方法和以下方法:

public static long GetPrivateWorkingSetForAllProcesses(string ProcessName)
{
  long totalMem = 0;
  Process[] process = Process.GetProcessesByName(ProcessName);
  foreach (Process proc in process)
  {
    long memsize = GetProcessPrivateWorkingSet64Size(proc.Id);
    totalMem += memsize;
  }
  return totalMem;
}

最后我得到了输出,如果同一进程有多个实例…如记事本有4个实例,那么它将被视为记事本记事本# 1记事本# 2记事本# 3

好吧,我发布了我的代码,人们被要求调试自己的代码,并在我关闭它之前注释它。

public int m_PMmonitor()
    {
        int count = 0,sum=0;
        int[] lm = new int[10];
        while (true)
        {
            try
            {
                Process[] proc = Process.GetProcessesByName(InstanceName);
                PerformanceCounter PC;
                sum = proc.Count();
                int k = 0;
                string pname;
                foreach (var pro in proc)
                {
                    lm[count] = pro.Id;
                    if ((sum-1) == count)
                    {
                        for (int i = 0; i <= (sum - 1); i++)
                        {
                            Process p = Process.GetProcessById(lm[i]);
                            pname = p.ProcessName;
                            if (k == 0)
                            {
                                PC = new PerformanceCounter("Process", "Working Set - Private", pname, true);
                                memsize = Convert.ToInt32(PC.NextValue()) / 1024;
                                Console.WriteLine(memsize);
                                k++;
                            }
                            else
                            {
                                PC = new PerformanceCounter("Process", "Working Set - Private", pname + "#" + (k).ToString(), true);
                                memsize = Convert.ToInt32(PC.NextValue()) / 1024;
                                Console.WriteLine(memsize);
                                k++;
                            }
                            memsize = Convert.ToInt32(PC.NextValue()) / 1024;
                        }
                    }
                    count++;
                }
                Thread.Sleep(800);
                return memsize ;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Thread.Sleep(800);
                return 0;
            }
        }
    }

好的,下面是优化后的代码,这是为运行N个实例的进程检查"工作集-私有"。例如,假设Notepad的4实例正在运行,那么它将被视为记事本记事本# 1记事本# 2记事本# 3

注意:InstanceName名称没有扩展名,您可以通过任务管理器验证它。"Working Set - Private"将在编码时使用,但是为了验证,您可以在任务管理器

的进程选项卡下检查列内存(私有工作集)
 public int m_PMmonitor()
    {
        int count = 0, sum = 0,buff=0;
        while (true)
        {
            try
            {
                Process[] proc = Process.GetProcessesByName(InstanceName);
                PerformanceCounter PC;
                sum = proc.Count();
                int k = 0;
                foreach (var pro in proc)
                {
                            if (k == 0)
                            {
                                PC = new PerformanceCounter("Process", "Working Set - Private", InstanceName, true);
                            }
                            else
                            {
                                PC = new PerformanceCounter("Process", "Working Set - Private", InstanceName + "#" + (k).ToString(), true);
                            }
                            memsize = Convert.ToInt32(PC.NextValue()) / 1024;
                            Console.WriteLine(memsize);
                            buff = buff + memsize; //adding memsize of current running instance
                            PC.Dispose();
                            PC.Close();
                            count++;
                            k++;
                }
                Thread.Sleep(800);
                return buff;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Thread.Sleep(800);
                return 0;
            }
            }
        }

你贴的代码真是让人痛苦。试试这个:

public int m_PMmonitor() {
    var processes = Process.GetProcessByName("notepad.exe");
    int memory = 0;
    foreach (var process in processes) {
        memory += process.WorkingSet / (1024^2);  // convert bytes to MB
    }
return memory;
}

或者如果你喜欢LINQ:

var memory = (from p in Process.GetProcessesByName("notepad.exe") select p.WorkingSet).Sum();