ruby / json

JSON implementation for Ruby
https://ruby.github.io/json
Other
673 stars 326 forks source link

Fix memory leak when exception is raised during JSON generation #574

Closed peterzhu2118 closed 6 months ago

peterzhu2118 commented 6 months ago

If an exception is raised the FBuffer is leaked.

For example, the following script leaks memory:

o = Object.new
def o.to_json(a) = raise

10.times do
  100_000.times do
    begin
      JSON(o)
    rescue
    end
  end

  puts `ps -o rss= -p #{$$}`
end

Before:

31824
35696
40240
44304
47424
50944
54000
58384
62416
65296

After:

24416
24640
24640
24736
24736
24736
24736
24736
24736
24736