本文介绍了如何在C#中测量应用程序的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用System.Diagnostics.Process在c#中测量我的应用程序的内存使用情况



I try to measure memory usage of my application in c# using this using System.Diagnostics.Process

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long memoryUsage= currentProcess.WorkingSet64;







但我没有得到任何结果。如果您有任何想法,请告诉我。



非常感谢



我有什么试过:



流程currentProcess = System.Diagnostics.Process.GetCurrentProcess();

long memoryUsage = currentProcess.WorkingSet64;




but i didn;t get any results. if you have any ideas please let me know.

Thanks alot

What I have tried:

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long memoryUsage= currentProcess.WorkingSet64;

推荐答案

memory = GC.GetTotalMemory(true);




int[] gccounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
    gccounts[i] = GC.CollectionCount(i);


private string GetMemoryUsage() // KLUDGE but works
{
    try
    {
        string fname = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);

        ProcessStartInfo ps = new ProcessStartInfo("tasklist");
        ps.Arguments = "/fi \"IMAGENAME eq " + fname + ".*\" /FO CSV /NH";
        ps.RedirectStandardOutput = true;
        ps.CreateNoWindow = true;
        ps.UseShellExecute = false;
        var p = Process.Start(ps);
        if (p.WaitForExit(1000))
        {
            var s = p.StandardOutput.ReadToEnd().Split('\"');
            return s[9].Replace("\"", "");
        }
    }
    catch { }
    return "Unable to get memory usage";
}



这篇关于如何在C#中测量应用程序的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 23:36