intridea / multi_json

A generic swappable back-end for JSON handling.
http://rdoc.info/projects/intridea/multi_json
MIT License
756 stars 130 forks source link

Memory leak with MultiJSON.dump #204

Open sethboyles opened 2 years ago

sethboyles commented 2 years ago

In a Rails app serving JSON responses, we noticed the following memory leak:

allocated by memory (61079397) (in bytes)
==============================
  61079397  /var/vcap/data/packages/cloud_controller_ng/46a8cc86e43a32e41d5b5e91e6983e75542ed547/gem_home/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/adapters/oj.rb:56

object count (1)
==============================
  1  /var/vcap/data/packages/cloud_controller_ng/46a8cc86e43a32e41d5b5e91e6983e75542ed547/gem_home/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/adapters/oj.rb:56

Memory would not be freed when the request ended, and eventually we would have to restart our process. We noticed the same behavior when using yajl.

Bypassing MultiJSON entirely resolved the issue for us.

More details here: https://github.com/cloudfoundry/capi-release/issues/262#issuecomment-1218564266

rwz commented 2 years ago

This is unfortunate. Both Oj and yajl are native extensions written in C, so there's not much MultiJSON can do about it here.

I recommend switching to a different adapter or stop using MultiJSON altogether as an option to remediate memory leaks.

sethboyles commented 2 years ago

Well I think the memory leak is only in MultiJSON, to clarify. Using OJ.dump seems fine. It's only when using Oj via MultiJSON that we witness the memory leak. Sorry if that wasn't clear.