Search code examples
djangoapachewsgidjango-wsgi

memory usage never go down unless inactivity-timeout not set


Part of /etc/apache2/sites-enabled/myproject.conf:

WSGIDaemonProcess myporject user=tester group=tester processes=2 threads=5 python-eggs=xxx display-name=xxx

When one user started browsing the website, memory used was increased by 80M (from output of free -m) and the memory usage would not go down even though the user logged out and shut down the browser, unless running service apache2 restart. I tried worker mode and prefork mode, but still not released.

When inactivity-timeout set to 60 seconds, memory usage will go down 60 seconds after log out.

I am new to apache2 and wsgi configuration. I just wonder whether setting inactivity-timeout is the good way for the memory usage to go down? Any other important configuration missing?

Any comments welcomed. If more information needed, please tell me. Thanks


Solution

  • mod_wsgi starts two processes that run your Django program (actually these two processes are your Django program). When a request finishes, the process continues to run, being ready to serve a new request.

    The request you are referring to needs 80 MB of memory, so Python requests that amount of memory from the operating system, and the operating system gives it to Python. When the request finishes, most of that memory is not needed any more, and it is unused. However, Python does not release it to the operating system. When Python needs memory again, it will re-use these 80 MB. In most cases, this way of working is satisfactory; it's not a problem that Python does not release the memory, since it's going to re-use it, so normally you don't need to do anything.

    inactivity-timeout makes mod_wsgi restart the process after 60 seconds, so the new process has not yet served a request, so it's not consuming any significant amount of memory.

    See also Releasing memory in Python.