获取当前的Ruby进程内存使用情况 [英] Get current ruby process memory usage

查看:161
本文介绍了获取当前的Ruby进程内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于长时间运行的一部分,我想格式化Logger输出以包括当前的内存使用情况.

I'd like to format my Logger output to include the current memory usage, for part of a long-running process.

为此,Ruby是否内置了任何东西,有点像PHP的

Is there anything built-in to Ruby for this, a bit like PHP's memory_get_usage()? Or do I have to execute some shell commands to get it from ps?

推荐答案

一年前,当尝试解决此问题时,我进行了很多在线研究和API挖掘,并且只能通过对ps的系统调用来解决它

When trying to solve this problem a year ago, I did a lot of online research and API digging and was only able to solve it via a system call to ps.

在OS X 10.7.2和Red Hat 4.1.2-13(在EC2上)中:

In both OS X 10.7.2 and Red Hat 4.1.2-13 (on EC2):

pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)

这会获取并将进程的驻留内存大小(以千字节为单位)放入大小变量.

This fetches and places the resident memory size of the process in kilobytes into the size variable.

只需一点努力就可以清除,但是大多数时间是花在调用ps并捕获其输出上,所以我认为这不值得花时间.

With a little effort this could be cleaned up, but most of the time is spend calling ps and capturing its output, so I don't think it is worth the time.

这篇关于获取当前的Ruby进程内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆