473,385 Members | 1,693 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,385 software developers and data experts.

Why is this a mem leak?

Hi,

I saw an article online
(http://aspn.activestate.com/ASPN/Coo...n/Recipe/65333) where it
explains how one can find out if there's a memory leak in ones program.
How is there a memory leak in this:

# make a leak
l = []
l.append(l)
del l

??? Shouldn't deleting an object explicitly give the GC a better chance
to find the object? Won't deleting it cause it's reference to go down
(another question: which GC is python 2.3 using? Refcount or
Generational?). Isn't 'del' similar to 'free'ing in C??

Thanks,

Steve

Jul 18 '05 #1
4 1532

Steve wrote:
Hi,

I saw an article online
(http://aspn.activestate.com/ASPN/Coo...n/Recipe/65333) where it
explains how one can find out if there's a memory leak in ones program.
How is there a memory leak in this:

# make a leak
l = []
l.append(l)
del l

??? Shouldn't deleting an object explicitly give the GC a better chance
to find the object? Won't deleting it cause it's reference to go down
(another question: which GC is python 2.3 using? Refcount or
Generational?). Isn't 'del' similar to 'free'ing in C??

Thanks,

Steve


Another question:

I get the following when I set DEBUG in gc to DEBUG_LEAK:

GARBAGE:
gc: uncollectable <JCollector instance at 0x403a7f2c>
gc: uncollectable <NCollector instance at 0x403fa2cc>
gc: uncollectable <dict 0x403a9c64>
gc: uncollectable <dict 0x403f9934>
gc: uncollectable <instancemethod 0x403a5dec>
gc: uncollectable <instancemethod 0x403aa0f4>

this was after calling a method foo() which created two instances of a
single class, which then created the dictionaries and instancemethods
named above. After foo() ended, I would have thought that these
instances/objects would have been collected. I called gc.collect()
explicitly two times but the gc.garbage list still held the same list as
'garbage'. Does 'uncollectable' here means that these objects will never
be collected? Why is that? They're not bound by anything once foo()
exits. How can I make sure the memory allocated for this junk is deleted
(since I want to run foo() in a loop). Can someone please tell me what's
happening? Thanks,

Steve

Jul 18 '05 #2
(The below message confuses Python, the language, with the CPython 2.3
implementation)

In the absence of gc, l will never be collected. (well, "the thing l once
named", since it won't name anything directly after the "del" statement)

with cyclic GC plus refcounting, including Python 2.3 when built in the
default way, l will be collected.

"del l" has the effect of making the name "l" no longer refer to anything
and of decreasing the reference count of that object by 1. It does not
relate directly to C's free() or C++'s delete, because those release
the storage underlying the object, even if pointers to that object still
exist elsewhere in the program. CPython only releases the underlying
storage for the object when the refcount reaches 0, with "GC" being used
to find unreachable cycles and deal with them in some other way.

Jeff
Jul 18 '05 #3
[Steve <st***@hotmail.com>]
....
Another question:

I get the following when I set DEBUG in gc to DEBUG_LEAK:

GARBAGE:
gc: uncollectable <JCollector instance at 0x403a7f2c>
gc: uncollectable <NCollector instance at 0x403fa2cc>
gc: uncollectable <dict 0x403a9c64>
gc: uncollectable <dict 0x403f9934>
gc: uncollectable <instancemethod 0x403a5dec>
gc: uncollectable <instancemethod 0x403aa0f4>

this was after calling a method foo() which created two instances of a
single class, which then created the dictionaries and instancemethods
named above. After foo() ended, I would have thought that these
instances/objects would have been collected. I called gc.collect()
explicitly two times but the gc.garbage list still held the same list as
'garbage'. Does 'uncollectable' here means that these objects will never
be collected?
No, but it does mean it's very likely they'll never be collected --
unless you do something to make them collectable.
Why is that?
The gc module is documented in the Library Reference Manual. The docs
for gc.garbage there should answer all your questions here.
They're not bound by anything once foo() exits. How can I make sure the memory allocated for this junk is deleted
(since I want to run foo() in a loop).


Read the gc.garbage docs. Then don't create cycles containing objects
with __del__ methods, or, if you think you must, do what the docs say
to do <wink>.
Jul 18 '05 #4
In article <41********@clarion.carno.net.au>, Steve <st***@hotmail.com> wrote:
I saw an article online
(http://aspn.activestate.com/ASPN/Coo...n/Recipe/65333) where it
explains how one can find out if there's a memory leak in ones program.
How is there a memory leak in this:

# make a leak
l = [] 1 reference to the new list, in the namespace
under 'l'. Reference count == 1
l.append(l) 1 more reference to the list, in its own
[0] element. Reference count == 2 del l The reference in the namespace is removed.
Reference count = 1
??? Shouldn't deleting an object explicitly give the GC a better chance
to find the object? Won't deleting it cause it's reference to go down
(another question: which GC is python 2.3 using? Refcount or
Generational?). Isn't 'del' similar to 'free'ing in C??


Not really. `del` removes a reference, which usually
culminates in freeing the storage, after all the required
dels are done.

For no memory leak:

l = [] # ref count = 1, i.e. l
l.append (l) # ref count = 2, i.e. l and :itself:[0]
m = l # ref count = 3, i.e. l, :itself:[0] and m
del l # ref count = 2, i.e. :itself:[0] and m
del m[0] # ref count = 1, i.e. m
del m # ref count = 0, and the objects space can be collected

(Here :itself: is made-up language, no way a valid Python
identifier, but how else to discuss a reference that isn't
part of any namespace?)

Regards. Mel.
Jul 18 '05 #5

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

Similar topics

32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
1
by: tcl | last post by:
Questions on ostrstream. #1) do I have a memory leak as the control exits the scope { ostrstream os; os << "hello world" << endl << ends; }
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
14
by: Don HO | last post by:
Hi, We have found a memory leak on a php server after executing a php/mysql application. The configuration is php 4.3.1 + mysql on windows server 2000 with IIS. Does the memory leak come from...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
24
by: c language | last post by:
Hello all, I am using 'Valgrind' to fix the memory leaks of my programs. I am looking for other programs to see how they are detecting the problems. Does any of you have experience in working...
1
by: dh | last post by:
Will GC.GetTotalMemory(true) reliably tell if a console app could have a memory leak, given the nature of memory management of GC in .NET? Like calling it two times, each at the beginning and right...
6
by: antani | last post by:
Every time that I call a function, time for execution and memory allocation program increase. I use stl vector, and 5 array c++ style and I remove them in destructor. Can you suggest me a...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
0
by: hairobinson | last post by:
Can any one help me explaing about Kernel Memory leak? what is kernel memory leak? how do we debug Kernel level Memory Leak? Do we have standard tool for finding it? How do we differentiate Kernel...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.