Skip to content

Instantly share code, notes, and snippets.

@jhuckabee
Last active December 19, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhuckabee/5978668 to your computer and use it in GitHub Desktop.
Save jhuckabee/5978668 to your computer and use it in GitHub Desktop.
Trying to track down state machine memory leak on anonymous classes.
require 'state_machine'
# Without state machine
regular_class = Class.new
regular_class_id = regular_class.object_id
regular_class = nil
# With state machine
machine_class = Class.new
machine_class.state_machine(:initial => :parked) do
transition({:parked => :idling, :idling => :on, :on => :parked})
end
machine_class_id = machine_class.object_id
machine_class = nil
GC.start
GC.start # Must be called twice
begin
p "Regular class still exists: #{ObjectSpace._id2ref(regular_class_id)}"
rescue
p "Regular class garbage collected!"
end
begin
p "State machine class still exists: #{ObjectSpace._id2ref(machine_class_id)}"
rescue
p "State machine class garbage collected!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment