Provides the ability to visualise and query the references between objects in Ruby's heap by exporting the heap into a graph database (Neo4j).
Useful for investigating memory leaks, it allows one to:
- Search by class and analyze the references held in memory by that class
- Search for objects that may be leaking memory by aggregating their references
-
Add the Gem to your Gemfile
gem 'ruby_heap_graph', git: '[email protected]:joshuafleck/ruby_heap_graph.git'
-
Trigger the graph to be built in your application. You will be presented with a prompt you must accept before the export will proceed. Note: The initial installation of Neo4j may fail the first time with a
Directory not empty
error - re-run the build to get around this.RubyHeapGraph.build
-
Open Neo4j in your browser:
http://localhost:7474
. Use Cypher (Neo4j's query language) to query the graph.
It uses the Ruby ObjectSpace library to find all reachable objects in memory for each object in the Ruby process. It dumps the object ids, class names, and object references for each object to temporary file, then it runs the Neo4j import tool to load this information into a graph database. It also performs the following:
- Installs Neo4j
- Stops Neo4j during the import
- Removes any existing Neo4j database
- Restarts Neo4j once the import is complete
Finding objects of a particular class (ActionController::Railtie
):
MATCH (n:`ActionController::Railtie`) RETURN n LIMIT 25
Finding the objects with the most references:
MATCH ()-[r:REACHABLE_FROM]->(parent)
RETURN parent, COUNT(r)
ORDER BY COUNT(r) DESC
LIMIT 10