Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ruby on rails - sidekiq memory usage reset

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...