473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Memory leak/gc.get_objects()/Improved gc in version 2.5

I'm running a python program that simulates a wireless network
protocol for a certain number of "frames" (measure of time). I've
observed the following:

1. The memory consumption of the program grows as the number of frames
I simulate increases.

To verify this, I've used two methods, which I invoke after every
frame simulated:

-- Parsing the /proc/<pid>/status file as in:
http://aspn.activestate.com/ASPN/Coo.../Recipe/286222
-- Using ps vg | grep python | awk '!/grep/ {print " ",$8}' in an
os.system() call.

The memory usage vs. frame number graph shows some big "jumps" at
certain points, and, after a large number of frames, shows a steady
upward slope

2. I think I've verified that the objects I instantiate are actually
freed-- I'm therefore assuming that this "leak" is "caused" by
python's garbage collection mechanism. I count the number of objects I
generate that are being tracked by gc as follows:

gc.collect()
objCount = {}
objList = gc.get_objects()
for obj in objList:
if getattr(obj, "__class__", None):
name = obj.__class__.__name__
if objCount.has_key(name):
objCount[name] += 1
else:
objCount[name] = 1

for name in objCount:
print name, " :", objCount[name]

del objList

Running this snippet every hundred frames or so, shows that the number
of objects managed by gc is not growing.

I upgraded to Python 2.5. in an attempt to solve this problem. The
only change in my observations from version 2.4 is that the absolute
memory usage level seems to have dropped. However, I still see the
jumps in memory usage at the same points in time.

Can anybody explain why the memory usage shows significant jumps (~200
kB or ~500 kb) over time (i.e. "frames") even though there is no
apparent increase in the objects managed by gc? Note that I'm calling
gc.collect() regularly.

Thanks for your attention,

Arvind

Oct 9 '07 #1
3 2433

<cr*************@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...

Questions like this about memory consumption should start with the
information printed by the interactive interpreter on startup and
additional info about whether the binary is from stock CPython or has 3rd
party modules compiled in. The latter are typically the source of real
problems.

Oct 9 '07 #2
On 10/8/07, cr*************@gmail.com <cr*************@gmail.comwrote:
I'm running a python program that simulates a wireless network
protocol for a certain number of "frames" (measure of time). I've
observed the following:

1. The memory consumption of the program grows as the number of frames
I simulate increases.

To verify this, I've used two methods, which I invoke after every
frame simulated:

-- Parsing the /proc/<pid>/status file as in:
http://aspn.activestate.com/ASPN/Coo.../Recipe/286222
-- Using ps vg | grep python | awk '!/grep/ {print " ",$8}' in an
os.system() call.

The memory usage vs. frame number graph shows some big "jumps" at
certain points, and, after a large number of frames, shows a steady
upward slope
This would be expected if you're creating ever-larger amounts of
objects - python uses memory pools and as the number of simultaneous
objects increases, the size of the pool will need to increase. This
isn't expected if the total number of objects you create is pretty
much static, but the way you're trying to determine that is flawed
(see below).
2. I think I've verified that the objects I instantiate are actually
freed-- I'm therefore assuming that this "leak" is "caused" by
python's garbage collection mechanism. I count the number of objects I
generate that are being tracked by gc as follows:

gc.collect()
objCount = {}
objList = gc.get_objects()
for obj in objList:
if getattr(obj, "__class__", None):
name = obj.__class__.__name__
if objCount.has_key(name):
objCount[name] += 1
else:
objCount[name] = 1

for name in objCount:
print name, " :", objCount[name]

del objList

Running this snippet every hundred frames or so, shows that the number
of objects managed by gc is not growing.

I upgraded to Python 2.5. in an attempt to solve this problem. The
only change in my observations from version 2.4 is that the absolute
memory usage level seems to have dropped. However, I still see the
jumps in memory usage at the same points in time.

Can anybody explain why the memory usage shows significant jumps (~200
kB or ~500 kb) over time (i.e. "frames") even though there is no
apparent increase in the objects managed by gc? Note that I'm calling
gc.collect() regularly.
You're misunderstanding the purpose of Pythons GC. Python is
refcounted. The GC exists only to find and break reference cycles. If
you don't have ref cycles, the GC doesn't do anything and you could
just turn it off.

gc.get_objects() is a snapshot of the currently existing objects, and
won't give you any information about peak object count, which is the
most direct correlation to total memory use.
Thanks for your attention,

Arvind

--
http://mail.python.org/mailman/listinfo/python-list
Oct 9 '07 #3
On Oct 9, 7:54 am, "Chris Mellon" <arka...@gmail.comwrote:
On 10/8/07, crazy420fing...@gmail.com <crazy420fing...@gmail.comwrote:
I'm running a python program that simulates a wireless network
protocol for a certain number of "frames" (measure of time). I've
observed the following:
1. The memory consumption of the program grows as the number of frames
I simulate increases.
To verify this, I've used two methods, which I invoke after every
frame simulated:
-- Parsing the /proc/<pid>/status file as in:
http://aspn.activestate.com/ASPN/Coo.../Recipe/286222
-- Using ps vg | grep python | awk '!/grep/ {print " ",$8}' in an
os.system() call.
The memory usage vs. frame number graph shows some big "jumps" at
certain points, and, after a large number of frames, shows a steady
upward slope

This would be expected if you're creating ever-larger amounts of
objects - python uses memory pools and as the number of simultaneous
objects increases, the size of the pool will need to increase. This
isn't expected if the total number of objects you create is pretty
much static, but the way you're trying to determine that is flawed
(see below).
2. I think I've verified that the objects I instantiate are actually
freed-- I'm therefore assuming that this "leak" is "caused" by
python's garbage collection mechanism. I count the number of objects I
generate that are being tracked by gc as follows:
gc.collect()
objCount = {}
objList = gc.get_objects()
for obj in objList:
if getattr(obj, "__class__", None):
name = obj.__class__.__name__
if objCount.has_key(name):
objCount[name] += 1
else:
objCount[name] = 1
for name in objCount:
print name, " :", objCount[name]
del objList
Running this snippet every hundred frames or so, shows that the number
of objects managed by gc is not growing.
I upgraded to Python 2.5. in an attempt to solve this problem. The
only change in my observations from version 2.4 is that the absolute
memory usage level seems to have dropped. However, I still see the
jumps in memory usage at the same points in time.
Can anybody explain why the memory usage shows significant jumps (~200
kB or ~500 kb) over time (i.e. "frames") even though there is no
apparent increase in the objects managed by gc? Note that I'm calling
gc.collect() regularly.

You're misunderstanding the purpose of Pythons GC. Python is
refcounted. The GC exists only to find and break reference cycles. If
you don't have ref cycles, the GC doesn't do anything and you could
just turn it off.

gc.get_objects() is a snapshot of the currently existing objects, and
won't give you any information about peak object count, which is the
most direct correlation to total memory use.
Thanks for your attention,
Arvind
--
http://mail.python.org/mailman/listinfo/python-list
Chris,

Thanks for your reply.

To answer the earlier question, I used CPython 2.4.3 and ActivePython
2.5.1 in my analysis above. No custom modules added. Interpreter
banners are at the end of this message.

In my program, I do keep instantiating new objects every "frame".
However, these objects are no longer needed after a few frames, and
the program no longer maintains a reference to old objects. Therefore,
I expect the reference-counting mechanism built into python (whatever
it is, if not gc) to free memory used by these objects and return it
to the "pool" from which they were allocated. Further, I would expect
that in time, entire pools would become free, and these free pools
should be reused for new objects. Therefore the total number of pools
allocated (and therefore "arenas"?) should not grow over time, if
pools are being correctly reclaimed. Is this not expected behavior?

Also, since I sample gc.get_objects() frequently, I would expect that
I would stumble upon a "peak" memory usage snapshot, or at the very
least see a good bit of variation in the output. However, this does
not occur.

Finally, if I deliberately hold on to references to old objects,
gc.get_objects() clearly shows an increasing number of objects being
tracked in each snapshot, and the memory leak is well explained.

Python version info:

ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May 2 2007, 08:46:07)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2

AND

Python 2.4.3 (#1, Mar 14 2007, 19:01:42)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Arvind

Oct 9 '07 #4

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

Similar topics

2
by: Adam Deutsch | last post by:
I would like to ask some advice about tracking down memory leaks in Python code. We have a python application running on Python 2.0.1 in an embedded Linux environment (kernel version 2.4.7). We...
10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
0
by: Abhishek S | last post by:
Hi Nick, Thanks for reply... Please include me in reply. Currently i am not in the list (i will subscribe soon) I upgarded to 2.4.1 - still the same issue. Nick> Thats not a lot of leak -...
1
by: lthompson | last post by:
I have a rather large python application (uses around 40MB of memory to start) that gradually chews up memory over many hours. I've done a little googling around, but it looks like I'm faced with...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
0
by: greg.novak | last post by:
I am using Python to process particle data from a physics simulation. There are about 15 MB of data associated with each simulation, but there are many simulations. I read the data from each...
5
by: Giampaolo Rodola' | last post by:
Hi, I'm in a big trouble since I don't know how to find some memory leaks I just discovered in a program of mine. By putting: import gc gc.set_debug(gc.DEBUG_LEAK) ...at the end of a script...
0
by: bieffe62 | last post by:
On 21 Ott, 17:19, Rolf Wester <rolf.wes...@ilt.fraunhofer.dewrote: To be sure that the deallocated memory is not cached at some level to be reused, you could try someting like this: while...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.