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

Incremental memory consumption #76

Closed
rjurado01 opened this issue Oct 1, 2018 · 3 comments
Closed

Incremental memory consumption #76

rjurado01 opened this issue Oct 1, 2018 · 3 comments
Labels

Comments

@rjurado01
Copy link

rjurado01 commented Oct 1, 2018

System:

  • delayed_job (4.1.5)
  • delayed_job_mongoid (2.3.0)
  • mongoid (6.3.0)

Problem

After some time delayed_job process is taking all memory in development environment.

It seems that problem ocurrs only when we set preload_models: true into mongoid configuration file.

It could be related with this issues of delayed_job:

@dblock dblock added the bug? label Feb 27, 2019
@johnnyshields
Copy link
Collaborator

johnnyshields commented Apr 1, 2021

Try setting env var MALLOC_ARENA_MAX=2 and see if that helps. We applied that in production and it greatly tamed our memory usage.

@johnnyshields
Copy link
Collaborator

I'm closing this. The main problem is that DelayedJob itself is not memory efficient, it needs to implement Copy-on-Write (CoW) forking. I started a PR here for that but it's still WIP collectiveidea/delayed_job#1160

@johnnyshields
Copy link
Collaborator

johnnyshields commented Apr 1, 2022

By the way, here's a script I use to avoid Kubernetes OOM killer, for anyone interested:

# in an initializer

# Add workaround for Kubernetes OOMKiller which SIGKILLs the process
# See: https://grosser.it/2017/02/02/ruby-on-kubernetes-memory-gc-oomkilled/
delayed_job = caller.last =~ %r{scripts?/delayed_job} || ((File.basename($PROGRAM_NAME) == 'rake') && ARGV[0].include?('jobs:work'))
k8s = OS.linux? && ENV.keys.any? {|k| k.start_with?('KUBERNETES') }
if delayed_job && k8s
  Thread.new do
    loop do
      used = Integer(File.read('/sys/fs/cgroup/memory/memory.usage_in_bytes')) / 1024 / 1024
      max = Integer(`cat /sys/fs/cgroup/memory/memory.stat | grep hierarchical_memory_limit`.split.last) / 1024 / 1024
      # puts "Ram: #{used}/#{max} MB"
      raise "Out of memory: #{used}/#{max} MB" if used + 5 >= max
      GC.start
      sleep 60
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants