Skip to content

Gem memory at boot time #87

@jamescook

Description

@jamescook

Problem

The gem eagerly loads all API endpoint wrappers, which bloats the memory footprint when the gem is loaded. For our particular use-case, we only need a handful of API endpoints.

Measurements

Boot-time memory measurements as of gem version 2.34.0 using bundle exec derailed_benchmarks bundle:mem in our Rails application. Seems you need to re-run the command several times as the measurement is not very precise/consistent between invocations.

CUT_OFF=0.001 bundle exec derailed bundle:mem | grep chargebee

3.2188 MiB
4.0156 MiB
6.5313 MiB
3.5938 MiB
3.7344 MiB

Now, on the latest chargebee gem version (2.47.1), which compared to 2.34 has introduced several more API endpoint wrappers shows memory usage has increased:

9.75 MiB
8.625 MiB
9.2813 MiB
7.7031 MiB
11.2656 MiB

My refactoring of lib/chargebee.rb to use autoload (master...jamescook:chargebee-ruby:use-autoload). Here we can see memory usage drops sharply at boot time:

0.125 MiB
0.0469 MiB
0.125 MiB
0.1719 MiB

I only used autoload as a POC - zeitwerk is likely a better solution longterm but requires more code changes whereas using autoload I only had to modify lib/chargebee.rb - this is assuming the specs validated the changes thoroughly enough.

My machine specs:

  • Ruby 3.3.6
  • Apple M1 max

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions