Skip to content
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

Memory leak with ActiveAttr::BasicModel and Class.new #169

Open
rmadamanchi opened this issue Sep 17, 2018 · 0 comments
Open

Memory leak with ActiveAttr::BasicModel and Class.new #169

rmadamanchi opened this issue Sep 17, 2018 · 0 comments

Comments

@rmadamanchi
Copy link

We recently ran into a memory issue that I successfully traced back to the creation of an anonymous class (Class.new) based on a type that includes AttriveAttr::BasicModel.

Below is the simplest form of the scenario demonstrating the Class objects eating up memory. It looks like the Class objects are somehow being held on to - which might be ok in general, but not ideal in case of an anonymous class instance created each time.

Running this test for any class that does not involve AttriveAttr::BasicModel clears up the memory as expected.

I am not proposing that creating anonymous classes like this is a best practice. We removed this pattern from our code base - but ideally, this shouldn't have happened.

require 'active_attr'

class MyClass
  include ActiveAttr::BasicModel
end

def class_object_count
  ObjectSpace.count_objects[:T_CLASS].to_s
end

puts "#{class_object_count} <- # Classes Before"

100000.times {Class.new(MyClass)}
puts "#{class_object_count} <- # Classes After"

GC.start
puts "#{class_object_count} <- # Classes After GC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant