473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Ian
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.
Jul 18 '05 #1
3 2581

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_stat us)
v = [v for v in t.readlines() if v.startswith(Vm Key)]
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 <40************ *********@ptn-nntp-reader02.plus.n et>, Ian
<in**@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.

Jul 18 '05 #2
Ian
Thats great, thanks, and it seems to work.

Unfortunately, my client needs to run on windows and mac (osX) too. I'm
presuming osX is similar to linux: I'll need to have a play to see if I can
get it working.

Is there a windows friendly way to do it?

Ian.

"Jean Brouwers" <JB************ ***********@no. spam.net> wrote in message
news:0907200418 15251751%JB**** *************** ****@no.spam.ne t...

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_stat us)
v = [v for v in t.readlines() if v.startswith(Vm Key)]
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 <40************ *********@ptn-nntp-reader02.plus.n et>, Ian
<in**@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.

Jul 18 '05 #3

Here is a slighly better version which avoids breaking the pseudo file
up into lines and parsing all lines. But still Linux only.

/Jean Brouwers
PropICy 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):
'''Private.
'''
global _proc_status, _scale
# get pseudo file /proc/<pid>/status
try:
t = open(_proc_stat us)
v = t.read()
t.close()
except:
return 0.0 # non-Linux?
# get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
t = v.index(VmKey)
v = v[t:].split(None, 3) # whitespace
if len(v) < 3:
return 0.0 # invalid format?
# convert Vm value to bytes
return float(v[1]) * _scale[v[2]]

def memory(since=0. 0):
'''Return memory usage in bytes.
'''
return _VmB('VmSize:') - since

def resident(since= 0.0):
'''Return resident memory usage in bytes.
'''
return _VmB('VmRSS:') - since

def stacksize(since =0.0):
'''Return stack size in bytes.
'''
return _VmB('VmStk:') - since

</pre>
In article <09************ *************** *************** @no.spam.net>,
Jean Brouwers <JB************ ***********@no. spam.net> wrote:
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_stat us)
v = [v for v in t.readlines() if v.startswith(Vm Key)]
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 <40************ *********@ptn-nntp-reader02.plus.n et>, Ian
<in**@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.

Jul 18 '05 #4

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

Similar topics

21
2817
by: Glen Wheeler | last post by:
Hello all, My program uses many millions of integers, and Python is allocating way too much memory for these. I can't have the performance hit by using disk, so I figured I'd write a C extension to define a new type. Problem is, my C knowledge is years old and regardless of my attempts distutils will not recognise my installation of the MS...
4
2150
by: MSUTech | last post by:
Hello All, I am getting an ASP error.. that tells the server it is OUT OF MEMORY.. then the server displays a message about the DLLHOST.exe and it waits for an "OK" from me... after hitting OK... it goes back to working fine.... BUT, while the notification is up on the screen... the website is NOT AVAILABLE.... that is the message that...
3
673
by: Robert | last post by:
Hi all, I am writing programs of algorithms using C++ and C. When I run the programs, I found the programs take 100 percent of the CPU usage and I cannot run any other program. How to control the CPU usage percentage? For example, let the program take 30 percent of the CPU usage? Thanks! BTW, the circumstance is Windows 2k and XP.
5
5773
by: Sharon | last post by:
I’m writing a Windows application. In the form I have a Panel and inside the panel I have a PictureBox control. I’m loading the PictureBox control with BMP image that has the following criteria: 14174 x 7874 Pixels (111.61MPixels), 1 BitsPerPixel, 3200x3200 DPI, No Compression. The memory consumption for loading this image should be...
1
1256
by: hape | last post by:
Hi all, i am looking for a way to get measurements for cpu and memory usage for ..net programs. The measurements have to be taken programmatically (monitoring). Thats why i cannot use tools like vadump a.s.o. Any Suggestions appreciated. Thanks in andvance.
1
1357
by: Daniel Mark | last post by:
Hello all: I have a python program and would like to find out the maximum memory used by this program. Does Python provide such module so I could check it easily?
0
1022
by: dariophoenix | last post by:
Does anyboy know if there is a way, in C++, to know how much free space left is there in the heap? (During runtime)
4
2784
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in t_Item class - I use it to store Item Objects (which are classes too). xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx class t_Item { public:
2
5053
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The application monitors all the textboxes and other input & display controls on that application. The data from the textboxes and listboxes are retrived fine....
0
7468
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
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...
0
7757
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...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
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...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1014
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.