Hooking into Python's memory management

Dan Stromberg drsalists at gmail.com
Wed May 4 14:06:37 EDT 2011


Sturla had some great comments; I'll add, in no particular order:

1) You could use the ctypes module to call the real malloc and free from
Python.
2) Yes, a Python "C extension module" can do explicit memory allocation.
3) Cython provides a language that is a hybrid of Python and C.  It might be
nice as a way of introducing explicit memory management.
4) You could also build a heap (not the tree kind, but the malloc kind) in
pure Python, and give it alloc and destroy operations.  Underneath it all,
things would still be reference counted/garbage collected, but that wouldn't
actually happen until you used your destroy.

On Wed, May 4, 2011 at 9:51 AM, Daniel Neilson <ddneilson at gmail.com> wrote:

  Part of our first year curriculum requires that students be exposed to
> explicit dynamic memory allocation in the form of C++'s new/delete, C's
> malloc/free, etc. I realize that Python is garbage collected, and that there
> is never a need to explicitly allocate & deallocate objects. However, I am
> trying to determine whether or not it is possible to simulate that behaviour
> within Python via a module for the purposes of instruction.
>
>  For these purposes, I would like to know whether it is possible within
> Python 3 to write a Python-only module that, essentially, hooks into the
> "constructor" and "destructor" of many of the Python built-in types
> (specifically list, dictionary, set, tuple, and string) so that the module
> can:
>  1) Maintain a list of object id()'s for objects that have been created.
> Ideally, this list would also contain the file & line number where the
> object was created.
>  2) Provide a "deallocate" function that will remove a given object's id()
> from the list from (1).
>  3) Print out an error message if the python script terminates with a
> non-empty list from (1). Preferably with a list of objects that are still
> "allocated."
>
>  Baring a Python-only module, would this behaviour be possible to add via a
> C-language module?
>
>  A module that hooked in to all memory allocation, and inspected the type
> of the object being allocated to conditionally add the object's id() to the
> list would also suffice.
>
>  In either case, if such a module is possible, any pointers you could
> provide regarding how to implement such a module would be appreciated.
>
> Thank you for your time,
>  Daniel
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110504/552708de/attachment-0001.html>


More information about the Python-list mailing list