reading the memory-space occupied by a PID

Stuart D. Gathman stuart at bmsi.com
Wed Jan 8 15:06:09 EST 2003


On Wed, 08 Jan 2003 09:21:19 -0500, Andras Schiff wrote:

> i want to write a program that gives out the the memory which is
> currently occupied by a given PID.
> With Unix-command 'top' you only get the maximal memory space wich was
> ever used by the PID but not the current one, which might be much
> smaller.

Top gives you the current memory space allocated by the process.  It so
happens, that a great many programs don't immediately allocated memory
back to the OS when they are done with it.  In fact, most never give it
back at all - just add it to an internal free list.  Those that do give it
back, delay doing so since changing OS memory allocation is relatively
expensive.

Recent Java VMs release memory back to the OS when it hasn't been used in
a while. Does CPython do this?

> Iam an absolute Newbie in programming so i don't know if there is a way
> in Python?

By the nature of the problem, there is no way in any language without the
cooperation of the target program.  The OS doesn't keep track of how an
application uses its memory - it just shells it out.  If the process you
want to query is running CPython, you can write a little server that
listens on a socket and prints current memory stats to callers.  (The
same goes for any language, but it is very easy in Python.)

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Python-list mailing list