WebFaction
Community site: login faq

I have a Django application that keeps going over its memory limit and I cannot figure out how to trace this back. Are there good ways to find what causes a runaway process? Is there a way to automatically kill an Apache worker if they get over a certain memory limit?

asked 18 Jan '12, 15:22

tclancy's gravatar image

tclancy
86515
accept rate: 6%


You can debug Apache/mod_wsgi processes with gdb.

First, make a note of the process id and command of whatever process is using a lot of memory or CPU. You can get that info by running:

ps -u $USER -o pid,command

Next, invoke gdb with the full path to the command of that process. For example, if your username is "tclancy" and your app name is "django", you can attach to your Apache processes with gdb by running:

gdb /home/tclancy/webapps/django/apache2/bin/httpd.worker

Once you are in gdb, attach to the process you noted above. For example, if the process id was 12345:

attach 12345

Next, you need to see the active threads by running:

info threads

The threads will be shown in a numbered list - switch to the thread that has your process id. For example, if the thread was #4:

thread 4

Finally, generate a Python traceback for whatever that thread is chewing on:

call PyRun_SimpleString("import sys, traceback; sys.stderr=open('/home/tclancy/traceback.txt','w',0); traceback.print_stack()")

With any luck, /home/tclancy/traceback.txt will contain a traceback that shows you what was executing in the thread that was causing a problem.

This doesn't always work as described above, for example if Apache isn't actually spinning on a Python operation. However, you can still use gdb to poke around in Apache to try to see what's going on. More information is available here: Apache Debugging Guide.

Hope that helps!

permanent link

answered 18 Jan '12, 16:48

seanf's gravatar image

seanf ♦♦
11.9k21333
accept rate: 37%

edited 17 May '12, 18:36

Thanks for this awesome tip seanf. I did some more research and found a gdb script that helps automate this:

Download script and copy it to your gdb config file:

wget http://svn.python.org/projects/python/trunk/Misc/gdbinit
cat gdbinit >> ~/.gdbinit

Run gdb with the PID of your process and then run the pystack command:

gdb -p PID
pystack

Source: http://stackoverflow.com/a/618748

permanent link

answered 17 May '12, 18:33

Michael%20Bunsen's gravatar image

Michael Bunsen
413
accept rate: 50%

edited 17 May '12, 18:38

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×891
×221
×82
×9

question asked: 18 Jan '12, 15:22

question was seen: 5,272 times

last updated: 17 May '12, 18:38

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2017 PARAGON INTERNET GROUP LIMITED
WEBFACTION IS A SERVICE OF PARAGON INTERNET GROUP LIMITED
REGISTERED IN ENGLAND AND WALES 7573953 - VAT REGISTRATION NUMBER 182147021
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM