Memory Control - Can you get the memory usage of the interpreter?

Jean Brouwers JBrouwersAtProphICyDotCom at no.spam.net
Fri Jul 9 21:03:49 EDT 2004


Assuming you are using Linux, below is an example for the memory usage
and stack size of the current process.  See the man page for proc for
more details.

/Jean Brouwers
 ProphICy Semiconductor, Inc.

<pre>

import os

_proc_status = '/proc/%d/status' % os.getpid()  # Linux only?
_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
           'KB': 1024.0, 'MB': 1024.0*1024.0}

def _VmB(VmKey):
    global _scale
    try: # get the /proc/<pid>/status pseudo file
        t = open(_proc_status)
        v = [v for v in t.readlines() if v.startswith(VmKey)]
        t.close()
         # convert Vm value to bytes
        if len(v) == 1:
           t = v[0].split()  # e.g. 'VmRSS:  9999  kB'
           if len(t) == 3:  ## and t[0] == VmKey:
               return float(t[1]) * _scale.get(t[2], 0.0)
    except:
        pass
    return 0.0
 
def memory(since=0.0):
    '''Return process memory usage in bytes.
    '''
    return _VmB('VmSize:') - since
 
def stacksize(since=0.0):
    '''Return process stack size in bytes.
    '''
    return _VmB('VmStk:') - since

</pre>


In article <40ef2dc0$0$546$ed2619ec at ptn-nntp-reader02.plus.net>, Ian
<info at fretfarm.co.uk> wrote:

> Hi all,
> 
> I have a problem. I have an application which needs to work with a lot of
> data, but not all at the same time. It is arranged as a set of objects, each
> with lots of data that is created when the object is instantiated.
> 
> I'd ideally like to keep as many objects as possible in memory, but I can
> get rid of any object the program isn't currently using.
> 
> Is there any way I can access the amount of memory python is using? I can
> then decide when to give up objects to the gc.
> 
> I don't want to use weakref because the gc simply collects weakref'ed stuff
> whether memory is tight or not, and then I have to recreate it (which is
> costly). I only have one internal consumer for the data, and it is only
> working with one object at once, but may change to a different object at any
> time.
> 
> Thanks in advance
> 
> Ian.
> 
>



More information about the Python-list mailing list