memory_leak_callbacks
💡 Preventing Memory Leaks in Callback Procs​
Avoid capturing large objects in callback closures, which can prevent garbage collection. Use class_attribute
or method references instead of inline Procs to minimize retained context.
class Report < ApplicationRecord
class_attribute :notifier
self.notifier = ->(report_id) { ReportNotifier.notify(report_id) }
after_create -> { notifier.call(id) }
end
Here, the lambda only holds a reference to the class-level attribute, not each instance’s full context. This pattern reduces memory footprint in long-lived application processes.