Is there a way to retrieve a C# app's current memory usage?

核能气质少年 提交于 2019-12-28 06:17:52

问题


I am automating some profiling tasks and want to log heap space and generation sizes real-time. The profiling API seems awfully complicated for what I need, and it seems to listen in on individual allocations and collections, which isn't that important to me. Profiling tools are a great help of course, but I was looking for a more flexible, programmable interface.


回答1:


The term 'current memory usage' is a little loosely defined. Do you mean the working set? Whatever it means, you can use different properties such as VirtualMemorySize, WorkingSet, PrivateMemorySize, etc. from the process class to retrieve it.

long workingSet = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;



回答2:


There are performance counters for a lot of this stuff and if you can't use Perfmon, you can access counters through the Diagnostics API.




回答3:


Once I had to find a memory leak in a legacy code, I came accross this solution: Start "tasklist" with appropriate parameters as a process and read the output either from stream or from file.

e.g.

tasklist /fi "IMAGENAME eq notepad++.exe" /FO CSV /NH

Output is:

"notepad++.exe","7132","Console","1","21.004 K"

Not that elegant, but works in any programming language on Windows without additional dependences (C++/Qt in my case).



来源:https://stackoverflow.com/questions/461139/is-there-a-way-to-retrieve-a-c-sharp-apps-current-memory-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!