如何在C#中测量应用程序的内存使用情况 [英] How do I measure memory usage of my application in C#

查看:329
本文介绍了如何在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;

推荐答案

我想提出可能与您的代码目的不同的愿景。但它可能会引入一些有助于找到最佳解决方案的新点。



我的愿景是流程是操作系统的一部分。所以一个问题是为该进程分配了多少内存操作系统。 OS调用或外部工具在这里可能会有所帮助。



另一方面,具有自动内存管理(垃圾收集)的系统中的内存消耗取决于许多因素。在一种情况下,应用程序可以生成很多未处理的对象,GC不会释放它们。在其他情况下,GC可以积极释放内存。所以GC应该被视为活跃部分。



除了所有GC运行自己的进程。因此,如果你想评估整个应用程序的内存消耗,那么考虑在更广泛的环境中测量内存会更好,但不适用于一个进程。



我使用GC信息来评估内存消耗 - 可用内存和收集操作的数量。这是一些粗略的评估,但对某些情况可能有用。我的代码(例如/也与平台无关):

I would like to propose my vision that may differ from purpose of your code. But it could introduce some new points that could help to find the best solution.

My vision is that process is an OS part. So the one question is how much memory OS allocated for the process. OS calls or external tools could be helpful here.

In other hand memory consumption in systems with automatic memory management (Garbage Collection) is depends on many factors. In one case application can produce much not handled objects and GC do not release them. In other case GC can release memory aggressively. So GC should be taken into consideration as active part.

Besides all GC runs its own processes. So if you want to assess whole application memory consumption, the better considering measuring memory in more wide context, but not for one process.

I use GC information to assess memory consumption - memory available and number of collecting actions. It is some rough evaluation but could be useful for some cases. My codes (for example / also platform independent):
memory = GC.GetTotalMemory(true);




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


我使用它返回任务管理器的值:

I use this which returns the value from the task manager :
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";
}


我使用第三方工具:ReSharper dotMemory。请参阅 dotMemory:用于.NET的内存分析器和单元测试框架 [ ^ ]在他们的网站上。
I use a third party tool: ReSharper dotMemory. See dotMemory: Memory profiler and unit-testing framework for .NET[^] on their website.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆