3 ms·
I'd like to figure out where the memory consumption is. I feel like we could do a better job reducing the cold start memory footprint. I've done some stuff wi
by tenderlove 14y ago
I'd like to figure out where the memory consumption is. I feel like we could do a better job reducing the cold start memory footprint.
I've done some stuff with routes, but I feel there are other wins we could get. The good news is we have `ObjectSpace.memsize_of_all`:
irb(main):001:0> require 'objspace'
=> true
irb(main):002:0> ObjectSpace.memsize_of_all(String)
=> 169318
irb(main):003:0> x = 'x' * 1024; nil
=> nil
irb(main):004:0> ObjectSpace.memsize_of_all(String)
=> 171817
irb(main):005:0>
What's cool is that in MRI we can get size of the instruction sequences too:
irb(main):005:0> ObjectSpace.memsize_of_all(RubyVM::InstructionSequence)
=> 1600800
irb(main):006:0> 100.times { |i| eval("def x_#{i}; end") }
=> 100
irb(main):007:0> ObjectSpace.memsize_of_all(RubyVM::InstructionSequence)
=> 1711904
Also we can see the delta between methods defined with eval vs define_method:
irb(main):011:0> ObjectSpace.memsize_of_all(RubyVM::InstructionSequence)
=> 1717984
irb(main):012:0> 100.times { |i| define_method("x_#{i}") { } }
=> 100
irb(main):013:0> ObjectSpace.memsize_of_all(RubyVM::InstructionSequence)
=> 1722416
:-)
EDIT: stop saying "cool" so much. :-/
- oelmekki 14y agoI'm totally not in memory optimization, but I suppose an app memory size is directly proportional to its required gems' size ? Or is it in the contrary quite insignificant in runtime memory ?
- tenderlove 14y agoYa, it's definitely related to the number of files required. There are other factors though, like whether methods are defined via eval vs define_method, string allocations from stuff like database tables / column names.