Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reduce Ruby Memory Usage With These 12 Weird Tr...

Reduce Ruby Memory Usage With These 12 Weird Tricks

Nate Berkopec

November 11, 2016
Tweet

More Decks by Nate Berkopec

Other Decks in Programming

Transcript

  1. oink -- SUMMARY -- Worst Requests: 1. Feb 02 16:26:06,

    157524 KB, SportsController#show 2. Feb 02 20:11:54, 134972 KB, DashboardsController#show 3. Feb 02 19:06:13, 131912 KB, DashboardsController#show 4. Feb 02 08:07:46, 115448 KB, GroupsController#show 5. Feb 02 12:19:53, 112924 KB, GroupsController#show 6. Feb 02 13:03:00, 112064 KB, ColorSchemesController#show 7. Feb 02 13:01:59, 109148 KB, SessionsController#create 8. Feb 02 06:11:17, 108456 KB, PublicPagesController#join 9. Feb 02 08:43:06, 94468 KB, CommentsController#create 10. Feb 02 20:49:44, 82340 KB, DashboardsController#show
  2. memory_profiler allocated memory by gem ----------------------------------- rubygems x 305879 allocated

    memory by file ----------------------------------- /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb x 285433 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/basic_specification.rb x 18597 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems.rb x 2218 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/specification.rb x 1169 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/defaults.rb x 520 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/core_ext/kernel_gem.rb x 80 /home/sam/.rbenv/versions/2.1.0-github/lib/ruby/2.1.0/rubygems/version.rb x 80
  3. GC.stat - log it! { :count => 9, :heap_allocated_pages =>

    74, :heap_sorted_length => 75, :heap_allocatable_pages => 0, :heap_available_slots => 30164, :heap_live_slots => 29863, :heap_free_slots => 301, :heap_final_slots => 0, # etc etc }
  4. ObjectSpace.count_objects { :TOTAL => 30164, :FREE => 235, :T_OBJECT =>

    297, :T_CLASS => 944, :T_MODULE => 45, :T_FLOAT => 4, # etc etc }
  5. Solution 2c: If all else fails, move to Rake tasks

    Throwaway VMs are better than bloated VMs
  6. $ bundle exec derailed bundle:mem TOP: 54.1836 MiB mail: 18.9688

    MiB mime/types: 17.4453 MiB mail/field: 0.4023 MiB mail/message: 0.3906 MiB action_view/view_paths: 0.4453 MiB action_view/base: 0.4336 MiB
  7. sprockets/lib/sprockets/ autoload.rb module Sprockets module Autoload autoload :Babel, 'sprockets/autoload/babel' autoload

    :Closure, 'sprockets/autoload/closure' autoload :CoffeeScript, 'sprockets/autoload/coffee_script' autoload :Eco, 'sprockets/autoload/eco' #etc etc etc