Memory::Profiler
Efficient memory allocation tracking focused on retained objects only. Automatically tracks allocations and cleans up when objects are freed, giving you precise data on memory leaks.
Features
- Retained Objects Only: Uses
RUBY_INTERNAL_EVENT_NEWOBJandRUBY_INTERNAL_EVENT_FREEOBJto automatically track only objects that survive GC. - O(1) Live Counts: Maintains per-class counters updated on alloc/free - no heap enumeration needed!
- Tree-Based Analysis: Deduplicates common call paths using an efficient tree structure.
Usage
Please browse the source code index or refer to the guides below.
Getting Started
This guide explains how to use memory-profiler to automatically detect and diagnose memory leaks in Ruby applications.
Rack Integration
This guide explains how to integrate memory-profiler into Rack applications for automatic memory leak detection.
Releases
Please browse the releases for more details.
v1.5.1
- Improve performance of object table.
v1.5.0
- Add
Capture#each_objectfor getting all retained objects. - Add
retained_addresses:option toSampler#analyzeto capture addresses. - Add
Sampler#analyze(retained_minimum: 100)- if the retained_size is less than this, the analyse won't proceed. - Remove
Memory::Profiler::Graph- it's too slow for practical use. - Add
Memory::Profiler.address_of(object)to get the memory address of an object.
v1.4.0
- Implement Cooper-Harvey-Kennedy algorithm for finding root objects in memory leaks.
- Rework capture to track objects by
object_idexclusively.
v1.3.0
- Breaking: Renamed
Capture#count_fortoCapture#retained_count_offor better clarity and consistency. - Breaking: Changed
CallTree#top_paths(limit)toCallTree#top_paths(limit:)- now uses keyword argument. - Breaking: Changed
CallTree#hotspots(limit)toCallTree#hotspots(limit:)- now uses keyword argument. - Simplified
Sampler#analyzereturn structure to{allocations: {...}, call_tree: {...}}format. - Added
Allocations#as_jsonandAllocations#to_jsonmethods for JSON serialization. - Added
CallTree#as_jsonandCallTree#to_jsonmethods for JSON serialization with configurable options. Memory::Profiler::Allocations.newcan now be instantiated directly (primarily for testing).Sampler#statisticsis now a deprecated alias forSampler#analyze.- Breaking: Removed
Sampler#all_statisticsmethod.
v1.2.0
- Enable custom
depth:andfilter:options toSampler#track. - Change default filter to no-op.
- Add option to run GC with custom options before each sample to reduce noise.
- Always report sampler statistics after each sample.
v1.1.15
- Ignore
freeobjfor classes that are not being tracked.
v1.1.14
- Ignore
freeobjevents for objects with anonymous classes that are not tracked (and thus becomeT_NONE).
v1.1.13
- Fix sampler loop interval handling.
- Log capture statistics from sampler run loop.
v1.1.12
- Use
rb_obj_idfor tracking object states to avoid compaction issues.
v1.1.11
- Double buffer shared events queues to fix queue corruption.
Contributing
We welcome contributions to this project.
- Fork it.
- Create your feature branch (
git checkout -b my-new-feature). - Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin my-new-feature). - Create new Pull Request.
Developer Certificate of Origin
In order to protect users of this project, we require all contributors to comply with the Developer Certificate of Origin. This ensures that all contributions are properly licensed and attributed.
Community Guidelines
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.