Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory_profiler with File.read rather than string #8

Merged
merged 1 commit into from
Apr 12, 2017

Conversation

benoittgt
Copy link
Owner

I wanted to understand why https://github.com/benoittgt/understand_ruby_memory#why-when-using-a-frozen-string-we-dont-allocate-memory-

was displaying no allocation for :

report_3 = MemoryProfiler.report do
  ST_FREEZE = "yop".freeze
  def get_me_with_constant_freeze
    ST_FREEZE
  end
  get_me_with_constant_freeze
end

report_3.pretty_print
# Allocated String Report
# -----------------------------------
#
# Retained String Report
# -----------------------------------

So I change the benchmark and read a file instead of setting a string. It looks like this :

require 'memory_profiler'

report_1 = MemoryProfiler.report do
  def get_me_directly
    File.read('1.txt')
  end
  100.times { get_me_directly }
end

report_2 = MemoryProfiler.report do
  ST = File.read('2.txt')
  def get_me_with_constant
    ST
  end
  100.times { get_me_with_constant }
end

report_3 = MemoryProfiler.report do
  ST_FREEZE = File.read('3.txt').freeze
  def get_me_with_constant_freeze
    ST_FREEZE
  end
  100.times { get_me_with_constant_freeze }
end

puts ' With get_me_directly '.center(50, '✨')
report_1.pretty_print
# Allocated String Report
# -----------------------------------
#        200  "1.txt"
#        200  memory_freeze_benchmark.rb:5
#
#        100  ""
#        100  memory_freeze_benchmark.rb:5
#
#
# Retained String Report
# -----------------------------------

puts "\n"
puts ' With get_me_with_constant '.center(50, '✨')
report_2.pretty_print
# Allocated String Report
# -----------------------------------
#          2  "2.txt"
#          2  memory_freeze_benchmark.rb:11
#
#          1  ""
#          1  memory_freeze_benchmark.rb:11
#
#
# Retained String Report
# -----------------------------------
#          1  ""
#          1  memory_freeze_benchmark.rb:11

puts "\n"
puts ' With get_me_with_constant_freeze '.center(50, '✨')
report_3.pretty_print
# Allocated String Report
# -----------------------------------
#          2  "3.txt"
#          2  memory_freeze_benchmark.rb:19
#
#          1  ""
#          1  memory_freeze_benchmark.rb:19
#
#
# Retained String Report
# -----------------------------------
#          1  ""
#          1  memory_freeze_benchmark.rb:19

Results seems more coherent to me.

@benoittgt benoittgt changed the title Add memory_profiler with File.read than simple string Add memory_profiler with File.read rather than string Apr 11, 2017
@benoittgt benoittgt mentioned this pull request Apr 11, 2017
@benoittgt benoittgt merged commit de19879 into master Apr 12, 2017
@benoittgt benoittgt deleted the add_memory_benchmark_with_file_read branch April 12, 2017 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant