简体   繁体   English

如何测量给定时刻的进程内存使用情况

[英]How to measure process memory usage in given moment

Here is the way to measure peak memory usage of current process since the start of the process.这是自进程开始以来测量当前进程峰值内存使用量的方法。

process= psutil.Process(os.getpid())
process.memory_full_info().peak_wset

But what if I want to do few measurements for different parts(functions) of the program?但是,如果我想对程序的不同部分(功能)进行少量测量怎么办? How can I get memory used by program in any desired moment to check difference before and after?如何在任何需要的时刻获取程序使用的内存以检查前后差异? Maybe there is the way to reset peak_wset?也许有办法重置peak_wset?

Currently you no longer need os.getpid() when inspecting current process.当前检查当前进程时不再需要os.getpid() Just use psutil.Process()只需使用psutil.Process()

1) To measure if the peak memory increased or not (never decreased) before a function call, invoke this before and after a function call and take the difference: 1)要测量在函数调用之前峰值内存是否增加(从未减少),请在函数调用之前和之后调用 this 并取差异:

  • On Windows:在 Windows 上:
    • psutil.Process().memory_info().peak_wset
    • psutil.Process().memory_full_info().peak_wset
  • On Linux:在 Linux 上:
    • I read here the resource.getrusage(resource.RUSAGE_SELF).ru_maxrss would do the trick but I haven't tested.在这里读到resource.getrusage(resource.RUSAGE_SELF).ru_maxrss可以解决问题,但我还没有测试过。

2) To measure the current memory variation before and after a function call, invoke it before and after a function call and take the difference: 2) 要测量函数调用前后的当前内存变化,请在函数调用前后调用它并取差值:

  • On Windows and Linux:在 Windows 和 Linux 上:
    • psutil.Process().memory_info().rss
    • psutil.Process().memory_full_info().rss
    • psutil.Process().memory_full_info().uss

From docs :文档

memory_full_info() returns the same information as memory_info() , plus, on some platform (Linux, macOS, Windows), also provides additional metrics (USS, PSS and swap). memory_full_info()返回相同的信息memory_info()再加上一些平台(Linux操作系统,Mac系统,Windows)中,还提供了额外的度量(USS,PSS和swap)。 The additional metrics provide a better representation of “effective” process memory consumption (in case of USS) as explained in detail in this blog post.如本博客文章中详细解释的那样,附加指标更好地表示了“有效”进程内存消耗(在 USS 的情况下)。 It does so by passing through the whole process address.它通过传递整个进程地址来实现。 As such it usually requires higher user privileges than memory_info() and is considerably slower.因此,它通常需要比memory_info()更高的用户权限,并且速度要慢得多。 On platforms where extra fields are not implemented this simply returns the same metrics as memory_info() .在没有实现额外字段的平台上,这只是返回与memory_info()相同的指标。

uss (Linux, macOS, Windows): aka “Unique Set Size”, this is the memory which is unique to a process and which would be freed if the process was terminated right now. uss (Linux、macOS、Windows):又名“唯一设置大小”,这是一个进程独有的内存,如果进程现在终止,它将被释放。

Note : uss is probably the most representative metric for determining how much memory is actually being used by a process.注意uss可能是确定进程实际使用多少内存的最具代表性的指标。

3) To measure the specific memory a function call used during its execution, before any garbage collection takes place, you need a memory profiler . 3) 要测量函数调用在执行期间使用的特定内存,在进行任何垃圾收集之前,您需要一个内存分析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM