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 in Oj.dump if raise an exception #676

Merged
merged 1 commit into from Aug 2, 2021

Commits on Aug 2, 2021

  1. 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.
    Watson1978 committed Aug 2, 2021
    Copy the full SHA
    0ab66a6 View commit details
    Browse the repository at this point in the history