How do I find the memory used by a python process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Salim Fadhley

    How do I find the memory used by a python process

    I'm trying to create a python unit-test which will test a complex
    program which includes a number of functions which have been
    implemented in C or C++.

    The unit-test needs to check that after the functions have been run a
    few thousand times all of the memory used by those functions has been
    un-allocated, i.e. that there are no memory leaks.

    I was wondering if there is some way of finding out how much memory
    the current thread is using. I would like some kind of simple function
    call that gives me the current memory usage of the current process or
    thread.

    So each test would check the amount of memory available, call the
    function N times and then check the amount of memory available
    afterwards. If the amount of memory before and after changes by a
    certain amount then the test is failed.

    All of our unit-tests are single threaded processes. We run Windows
    and Python 2.4.

    Any suggestions? Thanks!

    Sal
  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: How do I find the memory used by a python process

    So each test would check the amount of memory available, call the
    function N times and then check the amount of memory available
    afterwards. If the amount of memory before and after changes by a
    certain amount then the test is failed.
    Please take a look at the muppy package:



    Regards,
    Martin

    Comment

    • Ben Finney

      #3
      Re: How do I find the memory used by a python process

      Salim Fadhley <salimfadhley@g mail.comwrites:
      The unit-test needs to check that after the functions have been run
      a few thousand times all of the memory used by those functions has
      been un-allocated, i.e. that there are no memory leaks.
      This is a great test case, thanks. I must add it to my store of
      generally-applicable unit test cases.

      However, I wonder why you specify the need to run the function
      *thousands* of times to test whether it releases memory. Surely your
      code unit is deterministic and will release memory (or not) the same
      way with the same inputs each time?

      --
      \ “The right to use [strong cryptography] is the right to speak |
      `\ Navajo.” —Eben Moglen |
      _o__) |
      Ben Finney

      Comment

      • Scott David Daniels

        #4
        Re: How do I find the memory used by a python process

        Ben Finney wrote:
        Salim Fadhley <salimfadhley@g mail.comwrites:
        >
        >The unit-test needs to check that after the functions have been run
        >a few thousand times all of the memory used by those functions has
        >been un-allocated, i.e. that there are no memory leaks.
        >
        This is a great test case, .... However, I wonder why you specify
        the need to run the function *thousands* of times to test whether
        it releases memory. Surely your code unit is deterministic and will
        release memory (or not) the same way with the same inputs each time?
        >
        Because simply using small integers will cause them to exist and live
        on in a cache. For this (and similar caching reasons), what you want
        to see is that memory doesn't grow in an unbounded way.


        --Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        Working...