百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

memorylogic

> 编程语言
开源

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

346 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

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

= Memory usage logger

This is linux specific. The following command retrieves the memory:

memory_usage = ps -o rss= -p #{$$}.to_i

The reason I created this was to help track down a memory leak. It worked very well because it adds the process id and memory usage in with EVERY single line logged and at the end of each rails request. By every line, I mean every line in your log will end with " (mem #{memory_usage})".

This way, if you have a cluster, you can monitor each process and scroll through your logs and watch the memory. If it suddenly starts to jump you can look at the request and at least you have a starting point. You can checkout the code for that specific controller action and try to narrow it down.

== Performance

Please note this is only for doing research. There is a cost to finding the current memory usage on every log output. The average cost is probably in the realm of 20ms per execution. Which means an extra 20ms every time a line is logged.

== Tips

Open up your log in a program that is made to read large files, NOT textmate. The console on the mac is a great program for this. To track a process id just do a "find next" with that process id. Watch the memory as you do this, if you have a true leak it will either gradually increase and never stop, or jump all at once. This will help you pin point the type of requests increasing your memory.

A great tool for hammering your server to find the memory leak yourself is the apache benchmarking tool. It should come pre-installed on your mac. Do something like:

ab -kc 10 -t 10 http://127.0.0.1:3000/

The above will create 10 threads to hammer your server for 10 seconds as fast as they can. This will go crazy, so be careful. If you have a bad memory leak this will consume your memory in no time. If you know your memory leak is bad, try starting out with something smaller, such as 2 seconds.

Lastly, if you notice your memory gradually increasing with each request, chances are it is something global in your application, such as a before_filter in your ApplicationController. Try eliminating global things until the leak goes away. If your memory leak is very abrupt, then you are lucky, because it should be easy to pinpoint. Again try changing or removing things in that specific action to see if that removes the leak.

If you are still lost and can't seem to pinpoint the problem, your best bet might be to create an entirely new app and move things over one by one until you can reproduce the leak. I know this is a pain in the ass, but sometimes it is the only way. It might take you a couple of hours, but you will find it. I've had my fair share of finding memory leaks and this method has yet to fail me.

== Other tools

Another tool you might find useful is: http://github.com/noahd1/oink/tree/master

It parses your logs and helps you find the problem in your logs. If your log is huge, this should be a quicker solution than manually searching your logs.

== Installation

class Applicationcontroller include Memorylogic end

Copyright (c) 2008 Ben Johnson of Binary Logic, released under the MIT license

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言