473,666 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 3 '08 #1
3 3169
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:

http://pypi.python.org/pypi/muppy

Regards,
Martin
Nov 3 '08 #2
Salim Fadhley <sa**********@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
Nov 3 '08 #3
Ben Finney wrote:
Salim Fadhley <sa**********@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
Sc***********@A cm.Org
Nov 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3039
by: lebo | last post by:
Hi I'm trying to understand how Python handles memory usage and dynamic object loading and unloading. Problem to solve? Build a very low memory footprint (non-GUI) Python application for Unix/Windows. Why use Python? End user flexibility. So far it seems that (on Windows): (1) memory increases when you import <module>. Expected behaviour. (2) memory does not release when you del <module>. Why not? Is Python
3
2033
by: Guy | last post by:
Hi It might take me a little time to explain this but here goes. Firstly I'm not using the latest upto date python releases so my first plan is to try more upto date rels of python and win32 libs, as this has fixed problems for me in the past (Don't like doing this as it usally breaks stuff and sys admin are slow to update software, although they do a cracking job, just covering my back.) I've created a build script for my companies...
8
3665
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has 256 MB of Ram while the other has 2 GB of Ram. On the machine with less Ram, the process takes about 1 MB of Ram. On the machine with more Ram, it uses 9 MB of Ram. Is this normal and expected behavior?
35
5491
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is indeed fixed. Being in a hurry, I originally used a q&d hack...: if sys.platform in ('linux2', 'darwin'): def _memsize(): """ this function tries to return a measurement of how much memory this process is consuming, in some arbitrary unit...
3
4794
by: James Aguilar | last post by:
Oh wise readers of comp.lang.python, Lend a newbie your ears. I have read several old articles from this group about memory mapping and interprocess communication and have Googled the sh** out of the internet, but have not found sufficient to answer my questions. Suppose that I am writing a ray tracer in Python. Well, perhaps not a ray tracer. Suppose that I am writing a ray tracer that has to update sixty times a second (Ignore...
1
2863
by: Andrew McLean | last post by:
I want to script the benchmarking of some compression algorithms on a Windows box. The algorithms are all embodied in command line executables, such as gzip and bzip2. I would like to measure three things: 1. size of compressed file 2. elapsed time (clock or preferably CPU) 3. memory used The first is straightforward, as is measuring elapsed clock time. But how would I get the CPU time used by a sub-process or the memory used?
5
3339
by: vishnu | last post by:
Hi there, I am embedding python 2.5 on embedded system running on RTOS where I had strict memory constraints. As python is a huge malloc intensive application, I observed huge memory fragmentation in my system which is leading to out of memory after running few scripts. So I decided to re-initialise the python with out restarting the whole python. I tried to use Py_Finalise() after completion of each script , then call Py_Initialise as...
1
3012
by: Nick Craig-Wood | last post by:
I've been dumping a database in a python code format (for use with Python on S60 mobile phone actually) and I've noticed that it uses absolutely tons of memory as compared to how much the data structure actually needs once it is loaded in memory. The programs below create a file (z.py) with a data structure in which looks like this -- z.py ---------------------------------------------------- z = {
13
2847
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure. It's most possibly caused by trac and it's dependencies, but several components of the OS where updated, too.
0
8454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.