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

Fix memory leak for filters with keyword arguments #157

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

peterzhu2118
Copy link
Member

The push_keywords_obj (which is a TypedData object) has its data pointer set to NULL and is never freed, which leaks memory. The following script demonstrates the issue:

require "liquid"
require_relative "lib/liquid/c"

1_000_000.times do
  Liquid::Template.parse("{{ value | default: 'None', allow_false: true }}")
end

puts `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i

Before this patch, the output is 199620, after this patch, the output is 124160.

We shouldn't be using rb_gc_force_recycle anyways because it doesn't clean up resources and we should let the GC decide when to free objects (rather than forcing the GC to reclaim the object).

@dylanahsmith
Copy link
Contributor

The push_keywords_obj (which is a TypedData object) has its data pointer set to NULL and is never freed, which leaks memory.

Good catch, I opened #158 as an alternative fix though.

We shouldn't be using rb_gc_force_recycle anyways because it doesn't clean up resources and we should let the GC decide when to free objects (rather than forcing the GC to reclaim the object).

It doesn't seem like that rb_gc_force_recycle is inherently bad (e.g. ruby uses it internally), but I had used it the wrong way. If I understand correctly, it doesn't require GC to reclaim the object, it is eagerly reclaiming the object so that it doesn't need GC to reclaim it.

@peterzhu2118
Copy link
Member Author

@dylanahsmith While there are several places in Ruby that use rb_gc_force_recycle, the Ruby core team does not like it as it is difficult to maintain and makes changes in the GC difficult (e.g. ruby/ruby#4329 (comment))

@dylanahsmith
Copy link
Contributor

Oh, apparently it is also "slow, now a day" according to the comment you linked to. Perhaps that is because it can't simply reuse objects with the current GC algorithm.

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

Successfully merging this pull request may close these issues.

2 participants