Re: [ctypes-users] freeing memory of ctypes python variables (Thomas Stover)
Brought to you by:
theller
From: Patricio S. <kp...@ho...> - 2011-01-13 01:45:17
|
I mean every call to do_buffer allocates more and more memory ! Give it a try and you'll see :) > From: cty...@li... > Subject: ctypes-users Digest, Vol 56, Issue 1 > To: cty...@li... > Date: Wed, 12 Jan 2011 12:09:50 +0000 > > Send ctypes-users mailing list submissions to > cty...@li... > > To subscribe or unsubscribe via the World Wide Web, visit > https://lists.sourceforge.net/lists/listinfo/ctypes-users > or, via email, send a message with subject or body 'help' to > cty...@li... > > You can reach the person managing the list at > cty...@li... > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of ctypes-users digest..." > > > Today's Topics: > > 1. Re: freeing memory of ctypes python variables (LukenShiro) > (Patricio Stegmann) > 2. Re: freeing memory of ctypes python variables (Thomas Stover) > 3. Re: pyusb under Cygwin and stdcall libusb-1.0 > [SEC=UNCLASSIFIED] (Xiaofan Chen) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Tue, 11 Jan 2011 14:26:48 -0200 > From: Patricio Stegmann <kp...@ho...> > Subject: Re: [ctypes-users] freeing memory of ctypes python variables > (LukenShiro) > To: ctypes users mailing list <cty...@li...> > Message-ID: <SNT...@ph...l> > Content-Type: text/plain; charset="iso-8859-1" > > > LukenShiro, > > Even using del l__buf, l__podata it wont free the memory. > The problem appears when I create a pointer to my variable. It doesnt free the memory. If no pointer is created, the memory is freed as soon as the function returns. Someone pointed me on usging gc.collect() ... however this seems an inelegant solution, and I suppose ctypes has something better to do that. > > > From: cty...@li... > > Subject: ctypes-users Digest, Vol 56, Issue 1 > > To: cty...@li... > > Date: Tue, 11 Jan 2011 12:07:52 +0000 > > > > Send ctypes-users mailing list submissions to > > cty...@li... > > > > To subscribe or unsubscribe via the World Wide Web, visit > > https://lists.sourceforge.net/lists/listinfo/ctypes-users > > or, via email, send a message with subject or body 'help' to > > cty...@li... > > > > You can reach the person managing the list at > > cty...@li... > > > > When replying, please edit your Subject line so it is more specific > > than "Re: Contents of ctypes-users digest..." > > > > > > Today's Topics: > > > > 1. freeing memory of ctypes python variables (Patricio Stegmann) > > 2. Re: freeing memory of ctypes python variables (LukenShiro) > > 3. pyusb under Cygwin and stdcall libusb-1.0 (Xiaofan Chen) > > 4. Re: pyusb under Cygwin and stdcall libusb-1.0 > > [SEC=UNCLASSIFIED] (Andrew MacIntyre) > > > > > > ---------------------------------------------------------------------- > > > > Message: 1 > > Date: Mon, 10 Jan 2011 17:31:13 -0200 > > From: Patricio Stegmann <kp...@ho...> > > Subject: [ctypes-users] freeing memory of ctypes python variables > > To: ctypes users mailing list <cty...@li...> > > Message-ID: <SNT...@ph...l> > > Content-Type: text/plain; charset="iso-8859-1" > > > > > > Hello to all, > > > > I am looking for a way to free the memory of some variables on a ctypes wrapper I have. > > I saw that if the code is using python variables, the python's gc should free the memory. So for example if I create a buffer (via ctypes.create_string_buffer(100*1024*1024)) and return from that function, python will free automagically that memory. > > > > See the above example which works fine: > > > > """ > > import ctypes > > > > def do_buffer(): > > l__buf = ctypes.create_string_buffer(100*1024*1024) > > > > def prompt_user(): > > l__res = '' > > while l__res != 'x': > > do_buffer() > > l__res = raw_input('press x to exit or other to re-get mem') > > > > if __name__ == '__main__': > > prompt_user() > > """ > > > > However if I create a pointer to that buffer, ctypes or python is not freeing the memory even after returning from the function. For example this new code never frees the memory: > > > > """ > > import ctypes > > > > def do_buffer(): > > l__buf = ctypes.create_string_buffer(100*1024*1024) > > l__podata = ctypes.cast(l__buf, ctypes.POINTER(ctypes.c_ubyte)) > > > > def prompt_user(): > > l__res = '' > > while l__res != 'x': > > do_buffer() > > l__res = raw_input('press x to exit or other to re-get mem') > > > > if __name__ == '__main__': > > prompt_user() > > """ > > > > I wonder how can I be able to free that buffer. > > This is happening to me on Windows (xp pro), with python "Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32". > > > > Thank you for any suggestion, > > > > -------------- next part -------------- > > An HTML attachment was scrubbed... > > > > ------------------------------ > > > > Message: 2 > > Date: Mon, 10 Jan 2011 22:52:35 +0100 > > From: LukenShiro <luk...@ng...> > > Subject: Re: [ctypes-users] freeing memory of ctypes python variables > > To: cty...@li... > > Message-ID: <20110110225235.71dcea21@moonchild.loc> > > Content-Type: text/plain; charset=US-ASCII > > > > Il giorno Mon, 10 Jan 2011 17:31:13 -0200 > > Patricio Stegmann <kp...@ho...> ha scritto: > > > I am looking for a way to free the memory of some variables on a > > > ctypes wrapper I have. I saw that if the code is using python > > > variables, the python's gc should free the memory. So for example if > > > I create a buffer (via ctypes.create_string_buffer(100*1024*1024)) > > > and return from that function, python will free automagically that > > > memory. > > [..] > > > However if I create a pointer to that buffer, ctypes or python is not > > > freeing the memory even after returning from the function. > > > > If I'm not mistaken, I guess removing with 'del' the related > > variable(s), whose allocated memory you want to free, should speed the > > garbage collector process. If it is kept referenced I doubt it will be > > ever freed. :) > > > > > > -- > > GNU/Linux * Slackware64 current > > LU #210970 SU #12583 LM #98222/#412913 > > > > > > > > > > ------------------------------ > > > > Message: 3 > > Date: Tue, 11 Jan 2011 10:40:12 +0800 > > From: Xiaofan Chen <xia...@gm...> > > Subject: [ctypes-users] pyusb under Cygwin and stdcall libusb-1.0 > > To: cty...@li... > > Message-ID: > > <AANLkTi=RpHk=env8Y2jfOWF=A9c...@ma...> > > Content-Type: text/plain; charset=ISO-8859-1 > > > > http://sourceforge.net/mailarchive/message.php?msg_id=26873757 > > There seems to be problems to get pyusb to work under Cygwin. > > > > The author asks if Cygwin libusb-1.0 should use stdcall or not? > > http://sourceforge.net/mailarchive/message.php?msg_id=26873777 > > > > Normal Python under Windows can support either cdecl (CDLL) or > > stdcall (WinDLL). The problem is that Cygwin Ctypes only supports > > cdecl. Therefore it is not possible to load cygusb-1.0.dll and pyusb's > > libusb-1.0 backend will not work under Cygwin as a result. > > > > Just wondering why Ctypes under Cygwin does not support > > WinDLL? Take note os.name == 'posix' under Cygwin Python. > > I have updated Cygwin installation to the latest version. > > > > -- > > Xiaofan > > > > > > > > ------------------------------ > > > > Message: 4 > > Date: Tue, 11 Jan 2011 16:24:54 +1100 > > From: "Andrew MacIntyre" <And...@ac...> > > Subject: Re: [ctypes-users] pyusb under Cygwin and stdcall libusb-1.0 > > [SEC=UNCLASSIFIED] > > To: "Xiaofan Chen" <xia...@gm...>, > > cty...@li... > > Message-ID: > > <A09...@ac...t> > > Content-Type: text/plain; charset=us-ascii > > > > From: Xiaofan Chen > > > > > Just wondering why Ctypes under Cygwin does not support > > > WinDLL? Take note os.name == 'posix' under Cygwin Python. > > > > That really is a question for the maintainer of Python on Cygwin. It > > may be because the necessary libffi plumbing isn't in yet place on that > > platform. > > > > -------------------------> "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: and...@ac... http://www.acma.gov.au/ > > > > > > 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. > > > > > > > > ------------------------------ > > > > ------------------------------------------------------------------------------ > > Gaining the trust of online customers is vital for the success of any company > > that requires sensitive data to be transmitted over the Web. Learn how to > > best implement a security strategy that keeps consumers' information secure > > and instills the confidence they need to proceed with transactions. > > http://p.sf.net/sfu/oracle-sfdevnl > > > > ------------------------------ > > > > _______________________________________________ > > ctypes-users mailing list > > cty...@li... > > https://lists.sourceforge.net/lists/listinfo/ctypes-users > > > > > > End of ctypes-users Digest, Vol 56, Issue 1 > > ******************************************* > > -------------- next part -------------- > An HTML attachment was scrubbed... > > ------------------------------ > > Message: 2 > Date: Tue, 11 Jan 2011 11:21:34 -0600 > From: Thomas Stover <th...@ws...> > Subject: Re: [ctypes-users] freeing memory of ctypes python variables > To: <cty...@li...> > Message-ID: <a44...@ma...> > Content-Type: text/plain; charset=UTF-8 > > > From: Patricio Stegmann <kp...@ho...> > ... > > However if I create a pointer to that buffer, ctypes or python is not > > freeing the memory even after returning from the function. For example > this > > new code never frees the memory: > > > > """ > > import ctypes > > > > def do_buffer(): > > l__buf = ctypes.create_string_buffer(100*1024*1024) > > l__podata = ctypes.cast(l__buf, ctypes.POINTER(ctypes.c_ubyte)) > > > > def prompt_user(): > > l__res = '' > > while l__res != 'x': > > do_buffer() > > l__res = raw_input('press x to exit or other to re-get mem') > > > > if __name__ == '__main__': > > prompt_user() > > """ > > Do you mean it always keeps at least one buffer, or every call to > do_buffer() allocates a new one that never goes away? > > Could it be explained by a memory pool in the gc? > > It does seems like I remember something about the python object > create_string_buffer() has an extra reference count. > > -- > www.thomasstover.com > NO CARRIER > > > > ------------------------------ > > Message: 3 > Date: Wed, 12 Jan 2011 09:51:15 +0800 > From: Xiaofan Chen <xia...@gm...> > Subject: Re: [ctypes-users] pyusb under Cygwin and stdcall libusb-1.0 > [SEC=UNCLASSIFIED] > To: Andrew MacIntyre <And...@ac...> > Cc: cty...@li... > Message-ID: > <AANLkTim2DZpdhEVJjpSS+Tu-051K0J0HEpHHA=+U...@ma...> > Content-Type: text/plain; charset=ISO-8859-1 > > On Tue, Jan 11, 2011 at 1:24 PM, Andrew MacIntyre > <And...@ac...> wrote: > > From: Xiaofan Chen > > > >> Just wondering why Ctypes under Cygwin does not support > >> WinDLL? Take note os.name == 'posix' under Cygwin Python. > > > > That really is a question for the maintainer of Python on Cygwin. ?It > > may be because the necessary libffi plumbing isn't in yet place on that > > platform. > > > > Thanks. I asked in the Cygwin mailing list and the answer suggested > that Cygwin Python should be able to support WinDLL technically > but it has not been implemented yet. > > Ref: http://cygwin.com/ml/cygwin/2011-01/msg00103.html > ************************************ > From: Reini Urban > Both libffi and Ctypes should support stdcall. > I'm helping out with the perl Ctypes equivalent, > and cdecl, stdcall and both 64bit fastcalls do > work perfectly there. > > It just looks like cygwin python has no WinDLL support yet. > *********************************** > > > > -- > Xiaofan > > > > ------------------------------ > > ------------------------------------------------------------------------------ > Protect Your Site and Customers from Malware Attacks > Learn about various malware tactics and how to avoid them. Understand > malware threats, the impact they can have on your business, and how you > can protect your company and customers by using code signing. > http://p.sf.net/sfu/oracle-sfdevnl > > ------------------------------ > > _______________________________________________ > ctypes-users mailing list > cty...@li... > https://lists.sourceforge.net/lists/listinfo/ctypes-users > > > End of ctypes-users Digest, Vol 56, Issue 1 > ******************************************* |