sidekiq memory usage reset

2020-02-12 10:45发布

问题:

I have Rails app which uses Sidekiq for background process. To deploy this application I use capistrano, ubuntu server and apache passenger. To start and restart Sidekiq I use capistrano-sidekiq gem. My problem is - when Sidekiq is running, amount of memory (RAM) used by Sidekiq is growing up. And when Sidekiq finished all processes (workers) it keeps holding a large amount of RAM and not reseting it.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ubuntu    2035 67.6 45.4 3630724 1838232 ?     Sl   10:03 133:59 sidekiq 3.5.0 my_app [0 of 25 busy]     

How to make Sidekiq to reset used memory after workers finished their work?

回答1:

Sidekiq uses thread to execute jobs. And threads share the same memory as the parent process. So if one job uses a lot of memory the Sidekiq process memory usage will grow up and won't be released by Ruby.

Resque uses another technique it executes every jobs in another process therefore when the job is done, the job's process exits and the memory is released.

One way to prevent your Sidekiq process from using too much memory is to use Resque's forking method.

You could have your job main method executed in another process and wait until that new process exits

ex:

class Job
  include Process

  def perform
    pid = fork do
      # your code
    end
    waitpid(pid)
  end
end