C# 如何获取应用程序使用的内存量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14032515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the amount of memory used by an application
提问by BennoDual
Possible Duplicate:
How to get memory available or used in C#
可能的重复:
如何在 C# 中获得可用或使用的内存
I want to visualize the memory which is used by my application in the statusbar of my application. I am searching for a memoryleak - but I don't know where. Now, my idea is to visualize the used memory in the statusbar so that I can see it while I am working with the application and find the part, where the problem occurs and then I can profiling this.
我想在我的应用程序的状态栏中可视化我的应用程序使用的内存。我正在寻找内存泄漏 - 但我不知道在哪里。现在,我的想法是在状态栏中可视化已用内存,以便我在使用应用程序时可以看到它并找到出现问题的部分,然后我可以对其进行分析。
Can someone give me some help, how can I get the used memory.
有人可以给我一些帮助,我怎样才能获得已用的内存。
采纳答案by geniaz1
You can use the following function (The true parameter tells the GC to perform a collection first):
您可以使用以下函数(true 参数告诉 GC 先执行收集):
long memory = GC.GetTotalMemory(true);
回答by dutzu
Why not just monitor the memory usage with the TaskManager or with more advanced tools from Sysinternalsand only after you determine what workflow raises the memory usage abnormally then profile the application with CLR Profiler or others.
为什么不使用 TaskManager 或来自Sysinternals 的更高级工具监控内存使用情况,并且只有在您确定哪些工作流异常引发内存使用情况后,然后使用 CLR Profiler 或其他工具分析应用程序。
回答by Rahul Ranjan
You can try GC.GetTotalMemory
:
你可以试试GC.GetTotalMemory
:
It Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval > before returning, to allow the system to collect garbage and finalize > objects.
它检索当前认为已分配的字节数。一个参数表示这个方法是否可以在返回之前等待很短的时间间隔,以允许系统收集垃圾并最终确定>对象。
or
或者
using System.Diagnostics;
Process currentProc = Process.GetCurrentProcess();
Once you have a reference to the current process, you can determine its memory usage by reading the PrivateMemorySize64 property.
获得对当前进程的引用后,您可以通过读取 PrivateMemorySize64 属性来确定其内存使用情况。
long memoryUsed = currentProc.PrivateMemorySize64;
回答by Fabio
If you want to monitor the memory used by your application, you do not need to write code for that. Just use the performance counters from Windows (http://www.codeproject.com/Articles/8590/An-Introduction-To-Performance-Counters). They'll provide the information you need with charts! There are lots of memory counters (http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx), one of them will have the data you need and I guess it'll help you to find out when your app starts to use more memory than it should.
如果要监视应用程序使用的内存,则无需为此编写代码。只需使用 Windows 中的性能计数器(http://www.codeproject.com/Articles/8590/An-Introduction-To-Performance-Counters)。他们将通过图表提供您需要的信息!有很多内存计数器(http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx),其中之一将包含您需要的数据,我想它会帮助您找出何时应用程序开始使用比应有的更多内存。
However, if you do need to put this information in your application, you can still use performance counters. .NET has classes in System.Diagnostics namespace to access their data. See this example: https://weblog.west-wind.com/posts/2014/Sep/27/Capturing-Performance-Counter-Data-for-a-Process-by-Process-Id
但是,如果您确实需要将此信息放入您的应用程序中,您仍然可以使用性能计数器。.NET 在 System.Diagnostics 命名空间中有类来访问它们的数据。请参阅此示例:https: //weblog.west-wind.com/posts/2014/Sep/27/Capturing-Performance-Counter-Data-for-a-Process-by-Process-Id