"Help needed - I don't understand how Python manages memory"

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 20 12:38:38 EDT 2008


En Sun, 20 Apr 2008 09:46:37 -0300, Hank @ITGroup <hank.infotec at gmail.com> escribió:

> ``starting python``                        # == Windows Task Manager:
> Python.exe  *4,076 *K memory-usage ==
>  >>> st1='abcdefg'*999999         # == 10,952 K ==
>  >>> del st1                                 # == *4,104*K ==
>  >>> st1='abcdefg'*999999         # == 10,952 K ==
>  >>> del st1                                 # == 4,104 K ==
>
>  >>> li = ['abcde']*999999          # == 8,024 K ==
>  >>> del li                                    # == *4,108* K ==
>
>  >>> from nltk import FreqDist         # == 17,596 ==
>  >>> fd = FreqDist()                        # == 17,596 ==
>  >>> for i in range(999999):fd.inc(i)  # == 53,412 ==
>  >>> del fd                                       # == *28,780* ==
>  >>> fd2 = FreqDist()                       # == 28,780 ==
>  >>> for i in range(999999):fd2.inc(i)  # == 53,412 ==
>  >>> del fd2        # == 28,780 K ==
>
>  >>> def foo():
> ...         fd3 = FreqDist()
> ...         for i in range(999999):fd3.inc(i)
>
>  >>>  foo()         # == *28,788* K ==
>
>  >>> def bar():
> ...         fd4 = FreqDist()
> ...         for i in range(999999):fd4.inc(i)
> ...         del fd4
>                          # == 28,788 K ==
>  >>> bar()         # == 28,788 K ==
>
>
> That is my question, after ``del``, sometimes the memory space returns
> back as nothing happened, sometimes not... ...
> What exactly was happening???

Apart from what everyone has already said, consider that FreqDist may import other modules, store global state, create other objects... whatever.
Pure python code should not have any memory leaks (if there are, it's a bug in the Python interpreter). Not-carefully-written C extensions may introduce memory problems.

-- 
Gabriel Genellina




More information about the Python-list mailing list