[Tutor] Python and memory allocation

Steven D'Aprano steve at pearwood.info
Sat Oct 26 14:20:16 CEST 2013


On Thu, Oct 24, 2013 at 05:21:56PM +0000, Dave Angel wrote:

> If you
> want a surprise, try the following simple program some time.
> 
> import sys
> print(sys.modules)
> 
> when I tried that interactively on 2.7, it printed some 240+ names.

Wow. What were you doing? Ah, I bet you had imported numpy or similar! 
numpy brings in a lot.

[steve at ando ~]$ python2.7 -c "import sys, numpy; print len(sys.modules)"
233


Here are the results you get with a freshly-started Python interpreter 
on Linux, for various versions of Python, excluding numpy. Starting with 
ancient Python 1.5, and going right up the most recent 3.4 alpha 
version.


[steve at ando ~]$ python1.5 -c "import sys; print len(sys.modules)"
12
[steve at ando ~]$ python2.4 -c "import sys; print len(sys.modules)"
29
[steve at ando ~]$ python2.5 -c "import sys; print len(sys.modules)"
27
[steve at ando ~]$ python2.6 -c "import sys; print len(sys.modules)"
30
[steve at ando ~]$ python2.7 -c "import sys; print len(sys.modules)"
39
[steve at ando ~]$ python3.2 -c "import sys; print(len(sys.modules))"
52
[steve at ando ~]$ python3.3 -c "import sys; print(len(sys.modules))"
54
[steve at ando ~]$ python3.4 -c "import sys; print(len(sys.modules))"
34

And a few others:

steve at orac:~$ jython -c "import sys; print len(sys.modules)"
31
steve at orac:~$ ipy -c "import sys; print len(sys.modules)"
21
steve at orac:~$ ipython -c "import sys; print len(sys.modules)"
269


ipython also brings in a lot of modules, even more than numpy. Oooh, now 
there's a thought!

steve at orac:~$ ipython -c "import sys, numpy; print len(sys.modules)"
397


-- 
Steven


More information about the Tutor mailing list