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

Update README.md #5

Merged
merged 1 commit into from
Apr 8, 2017
Merged

Update README.md #5

merged 1 commit into from
Apr 8, 2017

Conversation

nateberkopec
Copy link
Contributor

As for your final question, definitely ask Sam. I think result looks like a bug.

@benoittgt
Copy link
Owner

Thanks @nateberkopec. And yes first mention (allocated vs retained) was related to memory_profiler.

@benoittgt benoittgt merged commit 83a974f into benoittgt:master Apr 8, 2017
@benoittgt
Copy link
Owner

Sorry @SamSaffron to ping you but if you have any idea for the last question. 😉

@SamSaffron
Copy link

what is the "last question" ?

@benoittgt
Copy link
Owner

Hello
Sorry I should have mention the title of the question :
@SamSaffron https://github.com/benoittgt/understand_ruby_memory#why-when-using-a-frozen-string-we-dont-allocate-memory-
Thanks

@SamSaffron
Copy link

I think cause it was allocated earlier when the ruby code was parsed. You can confirm this by loading a file and putting the load in a mem profiler block.

@benoittgt
Copy link
Owner

Ok Sam will give a try.

@benoittgt
Copy link
Owner

I used File.read instead of a string a result seems coherent now :
#8

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

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.

3 participants