获取当前的 ruby 进程内存使用情况 [英] Get current ruby process memory usage
问题描述
我想格式化我的 Logger
输出,以包含当前内存使用情况,作为长期运行过程的一部分.
I'd like to format my Logger
output to include the current memory usage, for part of a long-running process.
Ruby 有没有为此内置任何东西,有点像 PHP 的 memory_get_usage()
?还是我必须执行一些 shell 命令才能从 ps
获取它?
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屋!