473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tracking down memory leaks?

I have an application with one function called "compute", which given a
filename, goes through that file and performs various statistical
analyses. It uses arrays extensively and loops alot. it prints the
results of it's statistical significance tests to standard out. Since
the compute function returns and I think no variables of global scope
are being used, I would think that when it does, all memory returns
back to the operating system.

Instead, what I see is that every iteration uses several megs more.
For example, python uses 52 megs when starting out, it goes through
several iterations and I'm suddenly using more than 500 megs of ram.

Does anyone have any pointers on how to figure out what I'm doing
wrong?

Thanks,
mohan

Feb 12 '06 #1
14 2731
Em Dom, 2006-02-12 Ã*s 05:11 -0800, MKoool escreveu:
I have an application with one function called "compute", which given a
filename, goes through that file and performs various statistical
analyses. It uses arrays extensively and loops alot. it prints the
results of it's statistical significance tests to standard out. Since
the compute function returns and I think no variables of global scope
are being used, I would think that when it does, all memory returns
back to the operating system.

Instead, what I see is that every iteration uses several megs more.
For example, python uses 52 megs when starting out, it goes through
several iterations and I'm suddenly using more than 500 megs of ram.

Does anyone have any pointers on how to figure out what I'm doing
wrong?
Have you tried to force a garbage collection? Try, for example, running
gc.collect() everytime the function returns. See
http://www.python.org/doc/current/lib/module-gc.html for more details.
Thanks,
mohan


Cya,
Felipe.

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação' . Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Feb 12 '06 #2
I *think* Python uses reference counting for garbage collection. I've
heard talk of people wanting to change this (to mark and sweep?).
Anyway, Python stores a counter with each object. Everytime you make a
reference to an object this counter is increased. Everytime a pointer
to the object is deleteted or reassigned the counter is decreased.
When the counter reaches zero the object is freed from memory. A flaw
with this algorithm is that if you create a circular reference the
object will never be freed. A linked list where the tail points to the
head will have a reference count of 1 for each node, after the head
pointer is deleted. So the list is never freed. Make sure you are not
creating a circular reference. Something like this:

a = [1, 2, 3, 4, 5, 6]
b = ['a', 'b', 'c', 'd']
c = [10, 20, 30, 40]

a[3] = b
b[1] = c
c[0] = a

the last assignment creates a circular refence, and until it is
removed, non of these objects will be removed from memory.

I'm not an expert on python internals, and it is possible that they
have a way of checking for cases like this. I think the deepcopy
method catches this, but I don't *think* basic garbage collection look
for this sort of thing.

David

Feb 12 '06 #3
On Sun, 12 Feb 2006 05:11:02 -0800, MKoool wrote:
I have an application with one function called "compute", which given a
filename, goes through that file and performs various statistical
analyses. It uses arrays extensively and loops alot. it prints the
results of it's statistical significance tests to standard out. Since
the compute function returns and I think no variables of global scope
are being used, I would think that when it does, all memory returns
back to the operating system.
I may be mistaken, and if so I will welcome the correction, but Python
does not return memory to the operating system until it terminates.

Objects return memory to Python when they are garbage collected, but not
the OS.

Instead, what I see is that every iteration uses several megs more.
For example, python uses 52 megs when starting out, it goes through
several iterations and I'm suddenly using more than 500 megs of ram.

Does anyone have any pointers on how to figure out what I'm doing
wrong?


How big is the file you are reading in? If it is (say) 400 MB, then it is
hardly surprising that you will be using 500MB of RAM. If the file is 25K,
that's another story.

How are you storing your data while you are processing it? I'd be looking
for hidden duplicates.

I suggest you re-factor your program. Instead of one giant function, break
it up into lots of smaller ones, and call them from compute. Yes, this
will use a little more memory, which might sound counter-productive at the
moment when you are trying to use less memory, but in the long term it
will allow your computer to use memory more efficiently (it is easier to
page small functions as they are needed than one giant function), and it
will be much easier for you to write and debug when you can isolate
individual pieces of the task in individual functions.

Re-factoring will have another advantage: you might just find the problem
on your own.
--
Steven.

Feb 12 '06 #4
"dm************ **@yahoo.com" wrote:
I'm not an expert on python internals, and it is possible that they
have a way of checking for cases like this. I think the deepcopy
method catches this, but I don't *think* basic garbage collection look
for this sort of thing.


http://www.python.org/doc/faq/genera...-manage-memory

</F>

Feb 12 '06 #5

MKoool wrote:
I have an application with one function called "compute", which given a
filename, goes through that file and performs various statistical
analyses. It uses arrays extensively and loops alot. it prints the
results of it's statistical significance tests to standard out. Since
the compute function returns and I think no variables of global scope
are being used, I would think that when it does, all memory returns
back to the operating system.

Instead, what I see is that every iteration uses several megs more.
For example, python uses 52 megs when starting out, it goes through
several iterations and I'm suddenly using more than 500 megs of ram.

Does anyone have any pointers on how to figure out what I'm doing
wrong?
Are you importing any third party modules? It's not unheard of that
someone else's code has a memory leak.

Thanks,
mohan


Feb 12 '06 #6
On Sun, 12 Feb 2006 06:01:55 -0800, dm************* *@yahoo.com wrote:
I *think* Python uses reference counting for garbage collection.
Yes it does, with special code for detecting and collecting circular
references.
I've
heard talk of people wanting to change this (to mark and sweep?).
Reference counting is too simple to be cool *wink*

[snip] Make sure you are not creating
a circular reference. Something like this:

a = [1, 2, 3, 4, 5, 6]
b = ['a', 'b', 'c', 'd']
c = [10, 20, 30, 40]

a[3] = b
b[1] = c
c[0] = a

the last assignment creates a circular refence, and until it is removed,
non of these objects will be removed from memory.
I believe Python now handles this sort of situation very well now.

I'm not an expert on python internals, and it is possible that they have
a way of checking for cases like this. I think the deepcopy method
catches this, but I don't *think* basic garbage collection look for this
sort of thing.


deepcopy has nothing to do with garbage collection.

This is where you use deepcopy:

py> a = [2, 4, [0, 1, 2], 8] # note the nested list
py> b = a # b and a both are bound to the same list
py> b is a # b is the same list as a, not just a copy
True
py> c = a[:] # make a shallow copy of a
py> c is a # c is a copy of a, not a itself
False
py> c[2] is a[2] # but both a and c include the same nested list
True

What if you want c to include a copy of the nested list? That's where you
use deepcopy:

py> d = copy.deepcopy(a )
py> d[2] is a[2]
False

--
Steven.

Feb 12 '06 #7

me********@aol. com wrote:
MKoool wrote:
I have an application with one function called "compute", which given a
filename, goes through that file and performs various statistical
analyses. It uses arrays extensively and loops alot. it prints the
results of it's statistical significance tests to standard out. Since
the compute function returns and I think no variables of global scope
are being used, I would think that when it does, all memory returns
back to the operating system.

Instead, what I see is that every iteration uses several megs more.
For example, python uses 52 megs when starting out, it goes through
several iterations and I'm suddenly using more than 500 megs of ram.

Does anyone have any pointers on how to figure out what I'm doing
wrong?


Are you importing any third party modules? It's not unheard of that
someone else's code has a memory leak.


- sounds like you're working with very large, very sparse matrices,
running LSI/SVD or a PCA/covariance analysis, something like that. So
it's a specialized problem, you need to specify what libs you're using,
what your platform / O/S is, py release, how you installed it, details
about C estensions, pyrex/psyco/swig, the more info you supply, the
mroe you get back.

- be aware there's wrong ways to measure memory, e.g. this long thread:
http://mail.python.org/pipermail/pyt...er/310121.html

Feb 12 '06 #8
>> I'm not an expert on python internals, and it is possible that they have
a way of checking for cases like this. I think the deepcopy method
catches this, but I don't *think* basic garbage collection look for this
sort of thing.


deepcopy has nothing to do with garbage collection.

This is where you use deepcopy:

py> a = [2, 4, [0, 1, 2], 8] # note the nested list
py> b = a # b and a both are bound to the same list
py> b is a # b is the same list as a, not just a copy
True
py> c = a[:] # make a shallow copy of a
py> c is a # c is a copy of a, not a itself
False
py> c[2] is a[2] # but both a and c include the same nested list
True

What if you want c to include a copy of the nested list? That's where you
use deepcopy:

py> d = copy.deepcopy(a )
py> d[2] is a[2]
False


What I ment is that deepcopy is recursive, and if you have a circular
reference in your data structure a recursive copy will become infinite.
I think deepcopy has the ability to detect this situation. So if it
could be detected for deepcopy, I don't see why it could not be
detected for garbage collection purposes.

David

Feb 12 '06 #9
Hi Steven,

Is there any way for making Python return memory no longer needed to
the OS? Cases may arise where you indeed need a big memory block
temporarily without being able to split it up into smaller chunks.
Thank you.
malv

Steven D'Aprano wrote:
Objects return memory to Python when they are garbage collected, but not
the OS.


Feb 12 '06 #10

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

Similar topics

5
1709
by: Valerie Hough | last post by:
Currently our client runs one particular C# .NET executable, and after a few hours performance slows to a crawl. It would be very useful for me to be able to rule in (or out) the possibility that it is a result of memory leakage. Can someone point me to an article that discusses how bad programming may produce memory leaks? The application is particularly list box intensive (owner drawn, so pens, fonts, and brushes abound - all those...
9
1215
by: Frank1213 | last post by:
In my ASP.NET application, we are noticing appreciable memory leaks and the main culprit appears to be System.String We use ResourceReader to read in a resource file and we close and dispose the resourcereader object also. We profiled the application using .NET Memory profiler and it appears that garbage collection does not appear to be happening. Even after closing all sessions memory for aspnet process remains pegged at the level it...
2
1227
by: James Hunter Ross | last post by:
Friends, I've been watching or W3WP process size (using Task Manager), and it grows and grows when using our application. I am the only one on our server. I'll end my session, either through timeout or logout, and the process size never goes down. I could understand that if the next time I logged in the size didn't change, implying that the process was big but had "headroom". But, things start growing again. I guess that we have...
10
418
by: darkStar_e2 | last post by:
Hi guys. I have an applications which unfortunately eating up all the computer resources. the this is that it is not releasing the resources (memory) that it has eaten after it was closed... how do i handle this...
6
2491
by: depkefamily | last post by:
I have a large C++ program using multiple vendors DLLs which is giving me a major headache. Under release mode I get exceptions thrown from what looks like a dereferenced corrupt pointer. The problem is that when I change the inputs the location of the corruption moves, looking to me like someone is trashing memory and then later I see the results. The location also shifts when I change some of the code, although I have been unable to...
5
1624
by: cnickl | last post by:
Some time ago I was looking for a cheap or free way to check an application written in C# and some unsafe code for memory leaks. Someone in this forum suggested DevPartner form Compuware, telling me that there is a free basic version out there. However I can’t find it. Nowhere on the Compuware website is a link to anything under $4000. Am I just missing something? Are there other ways to check for memory leaks than 3rd party products?
4
2880
by: p1r0 | last post by:
Hi I was wondering if any of you could recommend me a good tool for testing for memry leaks under Windows and/or Linux Thanks in advance p1r0
3
5327
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". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
22
2276
by: Frank Rizzo | last post by:
I have an object tree that is pretty gigantic and it holds about 100mb of data. When I set the top object to null, I expect that the .NET framework will clean up the memory at some point. However, I am looking at the Task Manager and I don't see the MemUsage column decreasing even after an hour or two. I know that TaskManager may not be the best place to see what is the true way to gauge memory usage and/or presense of memory leaks. So...
0
9563
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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
9997
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
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
9822
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...
0
8821
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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
5270
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...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.