1

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

BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

2

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.

Community
  • 1
  • 1
Antonis Christofides
  • 6,990
  • 2
  • 39
  • 57
  • Thanks. `inactivity-timeout` is the right way to release the memory assigned to python? any other better way to release them? – BAE Dec 12 '16 at 16:25
  • As I said, you normally don't need to release memory. If you really need to do so, you could use `inactivity-timeout`, but why do you want to do so? If your application uses an excessive amount of memory, what you should be doing is fix your application. – Antonis Christofides Dec 12 '16 at 16:27
  • @BAE Note that this is always a trade-off. If you release the memory, the next request will be significantly slower, as it has to reload and recalculate everything that was previously saved in that memory. – knbk Dec 12 '16 at 16:46