[Baypiggies] Discussion for newbies/beginner night talks - test results

Chad Netzer chad.netzer at gmail.com
Fri Feb 16 01:29:45 CET 2007


On 2/15/07, Dennis Reinhardt <DennisR at dair.com> wrote:
> At 02:21 PM 2/15/2007, Chad Netzer wrote:
>
> >Dennis, you said in your example, "The final time.sleep(2000) is to
> >allow time for garbage collection to reduce memory usage."  However,
> >that certainly will have no effect with CPython, nor do operating
> >system memory allocators work that way.
>
>
> I read at http://docs.python.org/lib/module-gc.html where it says
>
>          "tune the collection frequency"
>
> and other statements on this page to mean that the garbage collector makes
> periodic sweeps of object references.

Note that the "garbage collector module" in CPython refers to the
cyclic-reference garbage collector, not the normal reference count
garbage collecting system.  In general, objects are destroyed
immediately after their reference count drops to zero, rather than
when a separate garbage collector thread decides to mark them as
garbage.

Hence, if you want to collect any unreachable cycles, gc.collect()
should definitely replace any sleep() calls.

I'm happy to hear that, as of 2.5, CPython can now return blocks of
memory used to allocate small objects to the system on occasion.
Hmmm, let's see...

Cool!  I just tried it!  First, fire up python 2.4 (or less) and input this:


class Foo:
    pass

l = [Foo() for i in xrange(1000000)]


Now we have a list of small user created objects, and the Python
process on my machine is using 217 MB of virtual mem (and 184 MB real
mem).  If I delete the list:

del l

My process is now using 213 virtual (181 real), so although the memory
is available for reuse by Python's object allocator, it is not
returned to the system (ie. it is not free()'d).  This means I can
make the same list again:

l = [Foo() for i in xrange(1000000)]

and the python process now uses the same max amount of memory as
before (217 MB virtual/184MB real).


Now, trying the same thing in Python 2.5:

class Foo:
    pass

l = [Foo() for i in xrange(1000000)]

and I get 219 MB virtual memory used (185 MB real memory).  However,
now when I delete the list:

del l

My virtual memory usage drops back down to 37 MB (3 MB real), which is
practically what it started with.  So, although this is a contrived
small object usage example, I think it is kinda neat. :)

Chad

PS. Yes, Anna, this isn't really beginner night material; just fun to discuss.


More information about the Baypiggies mailing list