Hot*_*ter 53

请参阅此SO问题

再试试这个

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
Run Code Online (Sandbox Code Playgroud)

  • 对于这个博客http://blogs.msdn.com/salvapatuel/archive/2007/10/13/memory-working-set-explored.aspx工作集!=总进程内存 (6认同)

Ada*_*lph 32

如果您只想测量由某些不同操作引起的虚拟内存使用量的增加,您可以使用以下模式: -

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

// performs operations here

var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
Run Code Online (Sandbox Code Playgroud)

当然,这是假设您的应用程序在上述操作运行时不在其他线程上执行操作.

您可以VirtualMemorySize64使用您感兴趣的任何其他指标进行替换.请查看System.Diagnostics.Process类型以查看可用的指标.

  • 第一次,将对象放在可释放队列上,稍后完成.之后,它们是可收藏的. (6认同)
  • 为什么需要调用两次GC.Collect? (2认同)