Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:22:18+00:00 2026-06-10T18:22:18+00:00

Do you know about an efficient way to log memory usage of a django

  • 0

Do you know about an efficient way to log memory usage of a django app per request ?

I have an apache/mod_wsgi/django stack, which runs usually well, but sometimes one process ends up eating a huge lot of memory. The servers ends up being short on mem, swapping a lot, and services are dramatically slowed down.

This situation is quite hard to fix because I don’t know which request is to be blamed for this behavior, I can’t reproduce it.

I’d like to have something deployed in production which logs the memory usage of the process before and after each request, with minimal overhead.


Before I start reinventing the wheel, do the community of my fellow djangoists know any existing solution to address this problem ?
Advices, middleware, snippet or maybe apache log configuration appreciated.

What (I think) I don’t need is:

  • a set of dev-stage profiling/debugging tools, I already know some and I’d use them if I knew what to profile/debug, it looks a little bit too much to be forever monitoring services running in production. On top of that, what is usually displayed by those tol is a mem usage report of the code shred to pieces, It would really be helpful to just pinpoint the faulty request.
  • generic advices on how to optimize mem usage of a django app, well it’s always good to read, but the idea here is rather «how to efficiently track down requests which need to be optimized».

My closest search results:

  • Django / WSGI – How to profile partial request? My profiling tools are per-request but app runs out of memory before then
  • Django memory usage going up with every request
  • Average php memory usage per request?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:22:20+00:00Added an answer on June 10, 2026 at 6:22 pm

    A Django middleware for tracking memory usage and generating a usable result immediately, needs to hook both process request and process response. In other words, look at difference between start and finish of request and log a warning if exceeds some threshold.

    A complete middleware example is:

    import os
    import psutil
    import sys
    
    THRESHOLD = 2*1024*1024
    
    class MemoryUsageMiddleware(object):
    
        def process_request(self, request):
            request._mem = psutil.Process(os.getpid()).memory_info()
    
       def process_response(self, request, response):
            mem = psutil.Process(os.getpid()).memory_info()
            diff = mem.rss - request._mem.rss
            if diff > THRESHOLD:
                print >> sys.stderr, 'MEMORY USAGE %r' % ((diff, request.path),)
            return response
    

    This requires the ‘psutil’ module to be installed for doing memory calculation.

    Is brute force and can lead to false positives in a multithread system. Because of lazy loading, you will also see it trigger on first few requests against new process as stuff loads up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please let me know on the quickest and efficient way to compare
I'm looking for some general advice on the most efficient way to go about
i know there are tons of posts about sub-stringing, believe me i have searched
I would like to know the most efficient way of storing days of the
Is there a way to dump and update rrd file in memory. I know
I have an VB.NET app that writes the status to a log file in
I pretty much don't know anything about these two. Which one is better/more efficient
Whats the most efficient way of doing pagination in JSF 2.0 app? I use
I know about git diff branch1 branch2 . However, I would like to see
I know about the LINEABOVE and LINEBELOW styles, i was wondering if there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.