-
-
Notifications
You must be signed in to change notification settings - Fork 94
Add explicit eager_load! to rails preload.rb example #290
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
Conversation
|
I spent a little time searching to see if/how Puma handles this, but it seems a bit different, and the ActionCable example is interesting. I wouldn't think this would be necessary, but perhaps so! I'll give it a shot as well and report back... |
|
Here is my last 24h. I too am running YJIT. Additionally, I'm using jemalloc and I was able to increase |
|
Nice work, looking forward to seeing conclusive results. |
| # frozen_string_literal: true | ||
|
|
||
| require_relative "config/environment" | ||
| Rails.application.eager_load! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Rails.application.eager_load! | |
| # This will load *all* of your application code into memory before fork, regardless | |
| # of its usage (or not) in a web process. | |
| # Depending on the structure of code in your application, you may want to disable | |
| # this, however, this is probably not true for most rails applications. | |
| Rails.application.eager_load! |
|
fwiw, i believe rails is supposed to call i believe this is (also) called via |
|
also, i believe the expectation is that rails apps eager load in production, so your hypothetical case (where doing so would use more memory) is likely not only a rare one but also still typically expected to eager load. there are other ways to reduce the memory footprint for such applications. 👍 |
|
Yeah, I think this is likely safe to close, or maybe worth mention as an aside in the docs if we think it's got a wider use case? |
|
Yes, this is a red herring. I am unsure why the differences in memory allocation are surfacing for my application. I confirmed my application code is loaded by starting an irb session in a rails environment that has irb(main):003> require_relative "config/environment"
irb(main):004> defined? User
=> "constant" |





Types of Changes
Adds an
Rails.application.eager_load!to the Rails preload.rb example.I was seeing wild memory consumption in a rails v7.2 app running on heroku with
WEB_CONCURRENCY=3running Performance-M dynos. Memory usage would steadily increase, often rapidly after boot. I added this change the the preload.rb file and am now seeing much more consistent memory usage and growth. This was the only change introduced during the time below.I was surprised this worked! I expected
require "config/environment"to be sufficient, especially since my production environment has:Given
config/environmentcallsRails.application.initialize!I would have expected eager loading to happen then. I did find this example in the rails source which callseager_load!after requiringconfig/environment.I've only let this cook 12 hours, but my traffic and usages are pretty consistent with this app so I feel confident with my outcome thus far. I wonder if this change may be circumstantial and not needed with most apps, but thought a PR would be a good place to discuss.
Contribution