1 # Setup any specified hard limit on the virtual size of the process
2 if APP_CONFIG.include?('hard_memory_limit') and Process.const_defined?(:RLIMIT_AS)
3 Process.setrlimit Process::RLIMIT_AS, APP_CONFIG['hard_memory_limit']*1024*1024, Process::RLIM_INFINITY
6 # If we're running under passenger and a soft memory limit is
7 # configured then setup some rack middleware to police the limit
8 if APP_CONFIG.include?('soft_memory_limit') and defined?(PhusionPassenger)
9 # Define some rack middleware to police the soft memory limit
17 status, headers, body = @app.call(env)
19 # Restart if we've hit our memory limit
20 if resident_size > APP_CONFIG['soft_memory_limit']
21 Process.kill("USR1", 0)
24 # Return the result of this request
25 [status, headers, body]
29 # Read statm to get process sizes. Format is
30 # Size RSS Shared Text Lib Data
31 fields = File.open("/proc/self/statm") do |file|
32 fields = file.gets.split(" ")
35 # Return resident size in megabytes
36 return fields[1].to_i / 256
40 # Install the memory limit checker
41 Rails.configuration.middleware.use MemoryLimit