问题描述
我有使用 Sidekiq 进行后台进程的 Rails 应用程序.为了部署这个应用程序,我使用了 capistrano、ubuntu 服务器和 apache 乘客.要启动和重新启动 Sidekiq,我使用 capistrano-sidekiq gem.我的问题是 - 当 Sidekiq 运行时,Sidekiq 使用的内存 (RAM) 量会增加.当 Sidekiq 完成所有进程(worker)时,它会保持大量 RAM 并且不会重置它.
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]
如何让 Sidekiq 在工人完成工作后重置已用内存?
How to make Sidekiq to reset used memory after workers finished their work?
推荐答案
Sidekiq 使用线程来执行作业.线程与父进程共享相同的内存.因此,如果一项作业使用大量内存,Sidekiq 进程内存使用量将会增加并且不会被 Ruby 释放.
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 使用另一种技术执行另一个进程中的每个作业,因此当作业完成时,作业的进程退出并释放内存.
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.
防止您的 Sidekiq 进程使用过多内存的一种方法是使用 Resque 的分叉方法.
One way to prevent your Sidekiq process from using too much memory is to use Resque's forking method.
您可以在另一个进程中执行您的作业 main 方法,并等待该新进程退出
You could have your job main method executed in another process and wait until that new process exits
例如:
class Job
include Process
def perform
pid = fork do
# your code
end
waitpid(pid)
end
end
这篇关于sidekiq 内存使用重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!