ruby/ruby on rails 内存泄漏检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/161315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:42:30  来源:igfitidea点击:

ruby/ruby on rails memory leak detection

ruby-on-railsrubymemorymemory-leakscoding-style

提问by Josh Moore

I wrote a small web app using ruby on rails, its main purpose is to upload, store, and display results from xml(files can be up to several MB) files. After running for about 2 months I noticed that the mongrel process was using about 4GB of memory. I did some research on debugging ruby memory leaks and could not find much. So I have two questions.

我使用 ruby​​ on rails 编写了一个小型 Web 应用程序,其主要目的是上传、存储和显示 xml(文件最多可达到几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程使用了​​大约 4GB 的内存。我做了一些关于调试 ruby​​ 内存泄漏的研究,但找不到太多。所以我有两个问题。

  • Are there any good tools that can be used to find memory leaks in Ruby/rails?
  • What type of coding patterns cause memory leaks in ruby?
  • 有没有什么好的工具可以用来查找 Ruby/rails 中的内存泄漏?
  • 什么类型的编码模式会导致 ruby​​ 内存泄漏?

采纳答案by Dave Nolan

Some tips to find memory leaks in Rails:

在 Rails 中查找内存泄漏的一些技巧:

The first is a graphical exploration of memory usage by objects in the ObjectSpace.

第一个是对 ObjectSpace 中对象的内存使用情况的图形探索。

The last two will help you identify specific usage patterns that are inflating memory usage, and you can work from there.

最后两个将帮助您识别导致内存使用量膨胀的特定使用模式,您可以从那里开始工作。

As for specific coding-patterns, from experience you have to watch anything that's dealing with file io, image processing, working with massive strings and the like.

至于特定的编码模式,根据经验,您必须注意处理文件 io、图像处理、处理大量字符串等的任何事情。

I would check whether you are using the most appropriate XML library - ReXML is known to be slow and believed to be leaky (I have no proof of that!). Also check whether you can memoizeexpensive operations.

我会检查您是否使用了最合适的 XML 库——众所周知,ReXML 很慢并且被认为是有漏洞的(我没有证据证明这一点!)。还要检查您是否可以记住昂贵的操作。

回答by Daniel Beardsley

A super simple method to log memory usage after or before each request (only for Linux).

在每个请求之后或之前记录内存使用情况的超级简单方法(仅适用于 Linux)。

#Put this in applictation_controller.rb
before_filter :log_ram # or use after_filter
def log_ram
  logger.warn 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip
end

You might want to load up script/console and try the statement out first to make sure it works on your box.

您可能想要加载脚本/控制台并首先尝试该语句以确保它在您的机器上工作。

puts 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip

Then just monitor top, when a request makes your memory usage jump, go check the logs. This, of course, will only help if you have a memory leak that occurs in large jumps, not tiny increments.

然后只监控top,当一个请求让你的内存使用量跳跃时,去检查日志。当然,这只会在发生大跳跃而不是微小增量的内存泄漏时有所帮助。

回答by Jean

Memory leak is a problem in the current ruby implementation a good place to start about this is http://whytheluckystiff.net/articles/theFullyUpturnedBin.htmlWhytheluckystiff website doesn't exist anymore but you can find the original article here: https://viewsourcecode.org/why/hacking/theFullyUpturnedBin.html

内存泄漏是当前 ruby​​ 实现中的一个问题,一个很好的起点是 http://whytheluckystiff.net/articles/theFullyUpturnedBin.htmlWhytheluckystiff 网站不再存在,但您可以在这里找到原始文章:https:/ /viewsourcecode.org/why/hacking/theFullyUpturnedBin.html

for a more specific answer on problems with long running ruby processes see https://just.do/2007/07/18/heap-fragmentation-in-a-long-running-ruby-process/

有关长时间运行的 ruby​​ 进程问题的更具体答案,请参阅 https://just.do/2007/07/18/heap-fragmentation-in-a-long-running-ruby-process/

maybe you could give passenger (mod_rails) a try https://web.archive.org/web/20130901072209/http://nubyonrails.com/articles/ask-your-doctor-about-mod_rails

也许您可以尝试一下乘客(mod_rails)https://web.archive.org/web/20130901072209/http://nubyonrails.com/articles/ask-your-doctor-about-mod_rails

回答by Vetal4eg

You should give a look to ruby-prof.

你应该看看ruby-prof

回答by kohlerm

Switch to jruby and use the Eclipse Memory Analyzer. There's no comparable tool for Ruby at the moment.

切换到 jruby 并使用Eclipse Memory Analyzer。目前没有可比的 Ruby 工具。

回答by Sardathrion - against SE abuse

Now, you can run the following to get the memory in a format that R can read. I am assuming that your log line looks like:

现在,您可以运行以下命令以 R 可以读取的格式获取内存。我假设您的日志行如下所示:

1234567890 RAM USAGE: 27456K

Run this (or modify to suite):

运行这个(或修改为套件):

$ grep 'RAM USAGE' fubar.log | awk '{print s " "  " " ; s++}' | sed 's/K//g' > mem.log

Then you can run this:

然后你可以运行这个:

#!/bin/sh
rm -f mem.png
R --vanilla --no-save --slave <<RSCRIPT
    lst <- read.table("mem.log")
    attach(lst)
    m = memory / 1024.0
    summary(m)
    png(filename="mem.png", width=1024)
    plot(date, m, type='l', main="Memory usage", xlab="time", ylab="memory")
RSCRIPT

and get a nice graph.

并得到一个漂亮的图表。

回答by nothing-special-here

These gems worked for me:

这些宝石对我有用:

MemoryLogic

记忆逻辑

Adds in proccess id and memory usage in your rails logs, great for tracking down memory leaks

在 Rails 日志中添加进程 ID 和内存使用情况,非常适合跟踪内存泄漏

Oink

叮咚

Log parser to identify actions which significantly increase VM heap size

日志解析器以识别显着增加 VM 堆大小的操作