views:

205

answers:

2

I want my app to throw an MemoryError when its usage goes over 1GB. I'm running in WSGI daemon mode.

I see 3 places the memory limit could be:

  • apache.conf
  • wsgi somewhere
  • django configuration

but I can't find the right config options. In PHP you can do this with :

php_value memory_limit 1GB

in your apache.conf

+2  A: 

Use resource.setrlimit() with resource.RLIMIT_VMEM.

Ignacio Vazquez-Abrams
for some reason, resource doesn't have that on my debian system (python 2.5) `>>> dir(resource)['RLIMIT_AS', 'RLIMIT_CORE', 'RLIMIT_CPU', 'RLIMIT_DATA', 'RLIMIT_FSIZE', 'RLIMIT_MEMLOCK', 'RLIMIT_NOFILE', 'RLIMIT_NPROC', 'RLIMIT_OFILE', 'RLIMIT_RSS', 'RLIMIT_STACK', 'RLIM_INFINITY', 'RUSAGE_CHILDREN', 'RUSAGE_SELF', '__doc__', '__file__', '__name__', '__package__', 'error', 'getpagesize', 'getrlimit', 'getrusage', 'setrlimit', 'struct_rusage']` Which of those constants means the same thing?
Paul Tarjan
You could probably get away with `resource.RLIMIT_RSS`.
Ignacio Vazquez-Abrams
+1  A: 

Resource memory limits aren't implemented on most platforms even though C API definitions exist. As such, mod_wsgi doesn't try to implement such restrictions. If PHP is doing it, is is able to do so by virtue that that it is a more constrained and controlled environment than Python.

The only portable way is to have a separate daemon process running which runs 'ps' or uses '/proc' to monitor memory usage of processes and then send a SIGINT signal to those which go over some predefined value.

Graham Dumpleton
yikes, that sounds rough. Other people must solve this problem too right? Is there a standard daemon to do this job?
Paul Tarjan