Re: freeing the memory of a casted object ? [SEC=PERSONAL]

"Andrew MacIntyre" <[email protected]> Thu, 23 Jun 2011 08:49:04 +0000
Newsgroups gmane.comp.python.ctypes
Message-ID <A1E4E9D08D14924498E135FB4441EE8D07193BEC@act01exmbx01vp.internal.govt>
[apologies for top post]

On Windows 7 32bit, Patricio's code triggers a MemoryError when it exhausts the heap. gc is not kicking in here, as the number of deallocations isn't sufficient to trigger it; reduce the buffer size to 1MB (and ditch the sleep) and gc does in fact kick in.

I think it comes down to the cast() function doing more under the covers than is explained in the docs, as it would appear that cast() results in an extra (weak?) reference being kept back to the original buffer.

This protectionism results in reference cycles, which gc.collect() can clean up.  This was probably to make certain operations feasible without lots of explicit references needing to be kept; see the type conversions section of the ctypes tutorial for an example that wouldn't work as intended/desired without this behind-the-scenes reference keeping.

Rather than routinely calling gc.collect(), tuning the gc thresholds for the particular usage pattern might be more attractive.  Either way, this is a case where actively using the gc module is required.

-------------------------> "These thoughts are mine alone!" <---------
Andrew MacIntyre           Operations Branch
tel:   +61 2 6219 5356     Communications Infrastructure Division
fax:   +61 2 6253 3277     Australian Communications & Media Authority
email: [email protected]<mailto:[email protected]>            http://www.acma.gov.au/

From: Diez B. Roggisch [mailto:[email protected]]
Sent: Thursday, 23 June 2011 5:06 PM
To: ctypes users mailing list
Subject: Re: [ctypes-users] freeing the memory of a casted object ?


On Jun 23, 2011, at 12:18 AM, Patricio Stegmann wrote:


Hello to all,

I use intensively ctypes for wrapping lot of cdll's and love the module !
However I face a painful problem of memory leak when I cast an object to something other.
I post the following example which running on win32 is perfectly stable in memory when not casting, but leask 10MB per cycle when casting.
I couldnt find help on the internet for this, nor found a free function on ctypes module or whatever.
Please see and test if possible the following code:

Under OSX I see a similar behavior, so at first I thought you were right.

But you aren't!

Python uses GC, and that's just not kicking in. If you reduce the wait-time to a .1 (so the memory consumption grows much faster) you will see that it peaks - and then a great deal will be lost again.

Alternatively, you can do

import gc


and then call gc.collect() every now and then. Then there should be no noticeable growth in mem-consumption.

But *don't* do this in real code unless you really have to. It just costs time. Let Python decide when to free memory.

Diez

NOTICE: This email message is for the sole use of the intended recipient(s) 
 and may contain confidential and privileged information. Any unauthorized 
 review, use, disclosure or distribution is prohibited. If you are not the 
 intended recipient, please contact the sender by reply email and destroy all 
 copies of the original message.

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
http://p.sf.net/sfu/quest-sfdev2dev

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users