Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

memory_profiler

> 编程语言
Open source

memory_profiler for ruby

1.8K stars0 likes3 views
WebsiteGitHub

About

memory_profiler for ruby

MemoryProfiler

A memory profiler for Ruby

Requirements

Ruby(MRI) Version 3.1.0 and above.

Installation

Add this line to your application's Gemfile:

gem 'memory_profiler'

And then execute:

$ bundle

Or install it yourself as:

$ gem install memory_profiler

Usage

There are two ways to use memory_profiler:

  • command line
  • convenience API

Command Line

The easiest way to use memory_profiler is via the command line, which requires no modifications to your program. The basic usage is:

$ ruby-memory-profiler [options] run [--] command [command-options]

Example:

$ ruby-memory-profiler --pretty run -- rubocop --cache false

$ ruby-memory-profiler --max=10 --pretty run -- ruby notify_users.rb 1 2 3 --quiet

For a full list of options, execute the following command:

$ ruby-memory-profiler -h

Convenience API

ruby
require 'memory_profiler'
report = MemoryProfiler.report do
  # run your code here
end

report.pretty_print

Or, you can use the .start/.stop API as well:

ruby
require 'memory_profiler'

MemoryProfiler.start

# run your code

report = MemoryProfiler.stop
report.pretty_print

NOTE: .start/.stop can only be run once per report, and .stop will be the only time you can retrieve the report using this API.

Options

report

The report method can take a few options:

  • top: maximum number of entries to display in a report (default is 50)
  • allow_files: include only certain files from tracing - can be given as a String, Regexp, or array of Strings
  • ignore_files: exclude certain files from tracing - can be given as a String or Regexp
  • trace: an array of classes for which you explicitly want to trace object allocations

Check out Reporter#new for more details.

…

pretty_print

The pretty_print method can take a few options:

  • to_file: a path to your log file - can be given a String
  • color_output: a flag for whether to colorize output - can be given a Boolean
  • retained_strings: how many retained strings to print - can be given an Integer
  • allocated_strings: how many allocated strings to print - can be given a Integer
  • detailed_report: should report include detailed information - can be given a Boolean
  • scale_bytes: flag to convert byte units (e.g. 183200000 is reported as 183.2 MB, rounds with a precision of 2 decimal digits) - can be given a Boolean
  • normalize_paths: flag to remove a gem's directory path from printed locations - can be given a Boolean Note: normalized path of a "location" from Ruby's stdlib will be prefixed with ruby/lib/. e.g.: ruby/lib/set.rb, ruby/lib/pathname.rb, etc.

Check out Results#pretty_print for more details.

For example to report to file, use pretty_print method with to_file option and path_to_your_log_file string:

$ pry
pry> require 'memory_profiler'
pry> MemoryProfiler.report(allow_files: 'rubygems'){ require 'mime-types' }.pretty_print(to_file: 'path_to_your_log_file')

$ less my_report.txt
Total allocated 82375
Total retained 22618

allocated memory by gem
-----------------------------------
rubygems x 305879

. . .

Example Session

You can easily use memory_profiler to profile require impact of a gem, for example:

…

The data is also available in the MemoryProfiler::Results object returned.

Retained vs Allocated

The report breaks down 2 key concepts.

Retained: long lived memory use and object count retained due to the execution of the code block.

Allocated: All object allocation and memory allocation during code block.

As a general rule "retained" will always be smaller than or equal to allocated.

Memory profiler will tell you aggregate costs of the above, for example requiring the mime-types gem above results in approx 2MB of retained memory in 22K or so objects. The actual RSS cost will always be slightly higher as MRI heaps are not squashed to size and memory fragments. In future we may be able to calculate a rough long term GC cost of retained objects (for major GCs).

Memory profiler also performs some String analysis to help you find strings that would heavily benefit from #freeze. In the example above the string IANA is retained in memory 2824 times, this costs you a minimum of RVALUE_SIZE (40 on x64) * 2824.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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