Title / Description
Code proc_val = nil report = MemoryProfiler.report do # A method that creates and returns a Proc. # It also uses a lot of memory before doing so def create_proc # Create an array with 10 million numbers large_array = Array.new(10000000, 0) # Use the array to do something useful # ... # Reset when done large_array = nil # Create and return Proc message = 'Hello World!' Proc.new { puts message } end proc_val = create_proc end report.pretty_print # Output: # Total allocated: 80000280 bytes (4 objects) # Total retained: 240 bytes (3 objects) allocated memory by gem ----------------------------------- 80000280 other allocated memory by file ----------------------------------- 80000280 (pry) allocated memory by location ----------------------------------- 80000040 (pry):19 200 (pry):24 40 (pry):23 allocated memory by class ----------------------------------- 80000040 Array 120 RubyVM::Env 80 Proc 40 String allocated objects by gem ----------------------------------- 4 other allocated objects by file ----------------------------------- 4 (pry) allocated objects by location ----------------------------------- 2 (pry):24 1 (pry):19 1 (pry):23 allocated objects by class ----------------------------------- 1 Array 1 Proc 1 RubyVM::Env 1 String retained memory by gem ----------------------------------- 80000280 other retained memory by file ----------------------------------- 80000280 (pry) retained memory by location ----------------------------------- 80000040 (pry):19 200 (pry):24 40 (pry):23 retained memory by class ----------------------------------- 80000040 Array 120 RubyVM::Env 80 Proc 40 String retained objects by gem ----------------------------------- 4 other retained objects by file ----------------------------------- 4 (pry) retained objects by location ----------------------------------- 2 (pry):24 1 (pry):19 1 (pry):23 retained objects by class ----------------------------------- 1 Array 1 Proc 1 RubyVM::Env 1 String Allocated String Report ----------------------------------- 1 "Hello World!" 1 (pry):23 Retained String Report ----------------------------------- 1 "Hello World!" 1 (pry):23 => nil [7] pry(main)> exit rapide-macbook:BankToTheFuture rapide$ rails c Running via Spring preloader in process 98250 Loading development environment (Rails 4.1.16) [1] pry(main)> proc_val1 = nil => nil [2] pry(main)> proc_val2 = nil => nil [3] pry(main)> require 'memory_profiler' => true [4] pry(main)> report = MemoryProfiler.report do [4] pry(main)* def create_proc [4] pry(main)* # Create an array with 10 million numbers [4] pry(main)* large_array = Array.new(10000000, 0) [4] pry(main)* # Use the array to do something useful [4] pry(main)* # ... [4] pry(main)* # Create and return Proc [4] pry(main)* message = 'Hello World!' [4] pry(main)* Proc.new { puts message } [4] pry(main)* end [4] pry(main)* proc_val1 = create_proc [4] pry(main)* # dupa [4] pry(main)* # dupa [4] pry(main)* proc_val2 = create_proc [4] pry(main)* end => #<MemoryProfiler::Results:0x3ffcca411610> [5] pry(main)> report.pretty_print Total allocated: 160000560 bytes (8 objects) Total retained: 160000560 bytes (8 objects) allocated memory by gem ----------------------------------- 160000560 other allocated memory by file ----------------------------------- 160000560 (pry) allocated memory by location ----------------------------------- 160000080 (pry):7 400 (pry):12 80 (pry):11 allocated memory by class ----------------------------------- 160000080 Array 240 RubyVM::Env 160 Proc 80 String allocated objects by gem ----------------------------------- 8 other allocated objects by file ----------------------------------- 8 (pry) allocated objects by location ----------------------------------- 4 (pry):12 2 (pry):11 2 (pry):7 allocated objects by class ----------------------------------- 2 Array 2 Proc 2 RubyVM::Env 2 String retained memory by gem ----------------------------------- 160000560 other retained memory by file ----------------------------------- 160000560 (pry) retained memory by location ----------------------------------- 160000080 (pry):7 400 (pry):12 80 (pry):11 retained memory by class ----------------------------------- 160000080 Array 240 RubyVM::Env 160 Proc 80 String retained objects by gem ----------------------------------- 8 other retained objects by file ----------------------------------- 8 (pry) retained objects by location ----------------------------------- 4 (pry):12 2 (pry):11 2 (pry):7 retained objects by class ----------------------------------- 2 Array 2 Proc 2 RubyVM::Env 2 String Allocated String Report ----------------------------------- 2 "Hello World!" 2 (pry):11 Retained String Report ----------------------------------- 2 "Hello World!" 2 (pry):11 => nil [6] pry(main)> exit rapide-macbook:BankToTheFuture rapide$ rails c Running via Spring preloader in process 98291 Loading development environment (Rails 4.1.16) [1] pry(main)> proc_val = nil => nil [2] pry(main)> report = MemoryProfiler.report do [2] pry(main)* def create_proc [2] pry(main)* # Create an array with 10 million numbers [2] pry(main)* large_array = Array.new(10000000, 0) [2] pry(main)* # Use the array to do something useful [2] pry(main)* # ... [2] pry(main)* # Reset when done [2] pry(main)* large_array = nil [2] pry(main)* [2] pry(main)* # Create and return Proc [2] pry(main)* message = 'Hello World!' [2] pry(main)* Proc.new { puts message } [2] pry(main)* end [2] pry(main)* proc_val = create_array [2] pry(main)* end NameError: uninitialized constant MemoryProfiler from (pry):2:in `<main>' [3] pry(main)> report = MemoryProfiler.report do [3] pry(main)* def create_proc [3] pry(main)* # Create an array with 10 million numbers [3] pry(main)* large_array = Array.new(10000000, 0) [3] pry(main)* # Use the array to do something useful [3] pry(main)* # ... [3] pry(main)* # Reset when done [3] pry(main)* large_array = nil [3] pry(main)* [3] pry(main)* # Create and return Proc [3] pry(main)* message = 'Hello World!' [3] pry(main)* Proc.new { puts message } [3] pry(main)* end [3] pry(main)* proc_val = create_proc [3] pry(main)* end NameError: uninitialized constant MemoryProfiler from (pry):17:in `<main>' [4] pry(main)> require 'memory_profiler' => true [5] pry(main)> report = MemoryProfiler.report do [5] pry(main)* def create_proc [5] pry(main)* # Create an array with 10 million numbers [5] pry(main)* large_array = Array.new(10000000, 0) [5] pry(main)* # Use the array to do something useful [5] pry(main)* # ... [5] pry(main)* # Reset when done [5] pry(main)* large_array = nil [5] pry(main)* [5] pry(main)* # Create and return Proc [5] pry(main)* message = 'Hello World!' [5] pry(main)* Proc.new { puts message } [5] pry(main)* end [5] pry(main)* proc_val = create_proc [5] pry(main)* end => #<MemoryProfiler::Results:0x3ffcca411610> [6] pry(main)> report.pretty_print Total allocated: 80000280 bytes (4 objects) Total retained: 240 bytes (3 objects) allocated memory by gem ----------------------------------- 80000280 other allocated memory by file ----------------------------------- 80000280 (pry) allocated memory by location ----------------------------------- 80000040 (pry):36 200 (pry):44 40 (pry):43 allocated memory by class ----------------------------------- 80000040 Array 120 RubyVM::Env 80 Proc 40 String allocated objects by gem ----------------------------------- 4 other allocated objects by file ----------------------------------- 4 (pry) allocated objects by location ----------------------------------- 2 (pry):44 1 (pry):36 1 (pry):43 allocated objects by class ----------------------------------- 1 Array 1 Proc 1 RubyVM::Env 1 String retained memory by gem ----------------------------------- 240 other retained memory by file ----------------------------------- 240 (pry) retained memory by location ----------------------------------- 200 (pry):44 40 (pry):43 retained memory by class ----------------------------------- 120 RubyVM::Env 80 Proc 40 String retained objects by gem ----------------------------------- 3 other retained objects by file ----------------------------------- 3 (pry) retained objects by location ----------------------------------- 2 (pry):44 1 (pry):43 retained objects by class ----------------------------------- 1 Proc 1 RubyVM::Env 1 String Allocated String Report ----------------------------------- 1 "Hello World!" 1 (pry):43 Retained String Report ----------------------------------- 1 "Hello World!" 1 (pry):43 => nil
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code