获取当前的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.
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屋!