Actions
Bug #22290
closedAdding instance variables to anonymous object may bloat all future classes
Bug #22290:
Adding instance variables to anonymous object may bloat all future classes
Description
Reproduction script:
o = Object.new
10.times do |i|
o.instance_variable_set("@a_#{i}", 1)
end
class Test
def initialize(a, b)
@a = a
@b = b
end
end
require 'objspace'
p ObjectSpace.memsize_of(Test.new(1, 2))
Expected:
Actual:
Explanation¶
Class records the max number of instance variables any of its instances held (max_iv_count).
When inheriting from a class, max_iv_count is copied in the child class.
If you add instance variables to an instance of Object, any future direct children of Object will start with a larger than needed max_iv_count and will cause Ruby to potentially allocate much larger objects than needed.
All versions since 3.2 are affected, the issue was fixed on master a couple months.
I believe we should backport the fix (I'm working on it), because otherwise some un-proper code running early in the process life-cycle can cause significant memory waste.
Updated by byroot (Jean Boussier) 3 days ago
- Status changed from Open to Closed
Backport pull requests:
Updated by nagachika (Tomoyuki Chikanaga) 1 day ago
- Backport changed from 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: WONTFIX, 3.4: DONE, 4.0: DONE
Merged backport PRs.
Actions