Background
My team is attempting to track down a memory leak in our Rails 6.1 application. We used the technique described here of taking three consecutive heap dumps and diffing them. We used rbtrace to get the dumps and rbheap to do the diffing. We tried this several times with different intervals between the samples.
Versions:
- Rails 6.1.6.1
- Ruby 3.0.3
Results
About 85% of the results in the diff look like the examples shown below. They are related to ActiveRecord's numerically validation, which we use in one of our models. This is the validation's source code. The strange thing is these allocations are IMEMO objects, which according to this article store information about the compiled code.
Validation in our model
validates :msrp, numericality: { less_than_or_equal_to: MAX_INT }, allow_nil: true
Example IMEMO object allocations
{
"address": "0x5632f3df7588",
"type": "IMEMO",
"class": "0x5632f654de48",
"imemo_type": "callcache",
"references": ["0x5632f654dbc8"],
"file": "/app/vendor/bundle/ruby/3.0.0/gems/activerecord-6.1.6.1/lib/active_record/validations/numericality.rb",
"line": 10,
"method": "validate_each",
"generation": 9233,
"memsize": 40,
"flags": {
"wb_protected": true,
"old": true,
"uncollectible": true,
"marked": true
}
}
{
"address": "0x5632f3e0f070",
"type": "IMEMO",
"class": "0x5632f7dc23d0",
"imemo_type": "callinfo",
"file": "/app/vendor/bundle/ruby/3.0.0/gems/activerecord-6.1.6.1/lib/active_record/validations/numericality.rb",
"line": 10,
"method": "validate_each",
"generation": 6225,
"memsize": 40,
"flags": {
"wb_protected": true,
"old": true,
"uncollectible": true,
"marked": true
}
}
Questions
- Has anyone witnessed similar behavior of memory leaks related to ActiveRecord validations?
- Does anyone have a theory as to why so many IMEMO objects are allocated and leaked for the same line of code?