A memory leak after an algorithm in Rails?

I wrote an algorithm inspired by the merge part of the merge sort merge.

  def self.merge(arr)
    if arr.length == 1
      return arr
    end
    groups = []
    (0...-(-arr.length/2)).each do |i|
      groups << []
      if !arr[2*i+1].nil?
        arr[2*i].each do |cal1|
          arr[2*i+1].each do |cal2|
            mergecal = func(cal1,cal2)
            if mergecal
              groups[i] << mergecal
            else
              mergecal = nil
            end
          end
        end
      else
        groups[i] = arr[2*i]
      end
    end
    arr = nil
    return merge(groups)
  end

After the page using this algorithm is displayed, the task manager will report 500 MB of RAM usage. Then, refreshing the same page again, the memory usage reached 1 GB. I tried adding GC.start(full_mark: true)to the controller right after the function call, but nothing changed. I'm not sure if a memory leak should be encoded with my code or with Ruby itself.

+4
source share
1 answer

Ruby , . , GC, . , , , . GC.stat, GC. , ruby ​​GC. .

0

Source: https://habr.com/ru/post/1666445/


All Articles