Git Product home page Git Product logo

memorylogic's Introduction

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: 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 © 2008 Ben Johnson of [Binary Logic](www.binarylogic.com), released under the MIT license

memorylogic's People

Contributors

binarylogic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.