Poll C# app's memory usage at runtime?

后端 未结 5 1092
傲寒
傲寒 2020-12-14 02:42

I have an app that, while running, needs to poll its own memory usage. It would be ideal if it could list out the memory usage for each object instantiated. I know this ca

相关标签:
5条回答
  • 2020-12-14 03:05

    Two functions you might find useful are:

      GC.GetTotalMemory();
      Process.PagedMemorySize64();
    

    My experience has been that GC.GetTotalMemory() is not terribly reliable. It often reports memory usage that is much smaller than the actual memory usage. I've seen it report that I'm using only 8 gigabytes when my program runs out of memory on a 16 gigabyte machine.

    I haven't yet tested Process.PagedMemorySize64, although it does look promising.

    0 讨论(0)
  • 2020-12-14 03:06

    You can get some coarse granularity about your process from System.Diagnostics, the Process class. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx.

    None of the 'per object' stuff, but at least some memory info about your process can be gleaned.

    0 讨论(0)
  • 2020-12-14 03:12

    You could listen on perfmon counters, which will give you plenty of data (GC activity / physical memory usage / managed heap etc..)

    If you need to go deeper you will probably have to attach a debugger to yourself, which is really super tricky cause you will have to spawn a new process and communicate with it, and walk through your memory.

    0 讨论(0)
  • 2020-12-14 03:20
    Process proc = Process.GetCurrentProcess();
    Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
    
    0 讨论(0)
  • 2020-12-14 03:30

    Maybe

    Windows::System::Diagnostics::ProcessDiagnosticInfo::GetForCurrentProcess();
    
    0 讨论(0)
提交回复
热议问题