Python等同于PHP的memory_get_usage()?

Python等同于PHP的memory_get_usage()?,第1张

Python等同于PHP的memory_get_usage()?

/proc/self/status
以下代码为Linux和其他系统提供了一个简单的解决方案,我在我的项目中使用了以下代码:

def memory_usage():    """Memory usage of the current process in kilobytes."""    status = None    result = {'peak': 0, 'rss': 0}    try:        # This will only work on systems with a /proc file system        # (like Linux).        status = open('/proc/self/status')        for line in status: parts = line.split() key = parts[0][2:-1].lower() if key in result:     result[key] = int(parts[1])    finally:        if status is not None: status.close()    return result

它返回当前和峰值驻留内存大小(这可能是人们在谈论应用程序使用多少RAM时的意思)。很容易扩展它以从

/proc/self/status
文件中获取其他信息。

出于好奇:的完整输出

cat /proc/self/status
如下所示:

% cat /proc/self/statusName:   catState:  R (running)Tgid:   4145Pid:    4145PPid:   4103TracerPid:      0Uid:    1000    1000    1000    1000Gid:    1000    1000    1000    1000FDSize: 32Groups: 20 24 25 29 40 44 46 100 1000 VmPeak:     3580 kBVmSize:     3580 kBVmLck:         0 kBVmHWM:       472 kBVmRSS:       472 kBVmdata:      160 kBVmStk:        84 kBVmExe:        44 kBVmLib:      1496 kBVmPTE:        16 kBThreads:        1SigQ:   0/16382SigPnd: 0000000000000000ShdPnd: 0000000000000000SigBlk: 0000000000000000SigIgn: 0000000000000000SigCgt: 0000000000000000CapInh: 0000000000000000CapPrm: 0000000000000000CapEff: 0000000000000000CapBnd: ffffffffffffffffCpus_allowed:   03Cpus_allowed_list:      0-1Mems_allowed:   1Mems_allowed_list:      0voluntary_ctxt_switches:        0nonvoluntary_ctxt_switches:     0


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5617004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存