Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leak in
Oj.dump
if raise an exception
If an exception occurs inside Oj.dump, the heap area has not been released. The heap area allocated by ALLOC_N must be released. This patch will fix this problem. ### Test code ```ruby require 'oj' data = { "foo" => "a" * 1024 * 1024 * 10, 1 => "Invalid hash key with strict mode" } 1000.times do |i| begin Oj.dump(data, mode: :strict) rescue end if i % 100 == 0 rss = Integer(`ps -o rss= -p #{Process.pid}`) / 1024.0 puts "#{i}, #{rss}" end end ``` ### Before ``` $ ruby json.rb 0, 49.4375 100, 1051.0 200, 1674.03125 300, 1773.625 400, 1822.171875 500, 1842.078125 600, 1841.015625 700, 1815.65625 800, 1781.53125 900, 1792.28125 ``` 1.8 GB of memory was used in my enviroment. ### After ``` $ ruby json.rb 0, 49.40625 100, 49.421875 200, 49.53125 300, 49.59375 400, 49.65625 500, 49.6875 600, 49.71875 700, 49.734375 800, 49.765625 900, 49.796875 ``` 50 MB of memory was used in my enviroment.
- Loading branch information