Memory grows (without bound); doesn't if code is in function

J

Jeff Epler

I wrote this program, and when I ran it the Python process grew to 200+
megs, and I was forced to kill it:
# Program 1: code at module level
import itertools
j = 0
for i in itertools.count():
j = j + str(i).count("1")
if j == i: print i

I changed the program so that the work is done in a function, and the
Python process stays small, <3 megs.
# Program 2: code at function level
import itertools
def main():
j = 0
for i in itertools.count():
j = j + str(i).count("1")
if j == i: print i
main()

I thought I had a pretty good internal model of Python, but apparently
I don't, because I can't explain the difference in behavior between the
two programs.

Can someone enlighten me?

Jeff
PS Python 2.3.2, RedHat 9

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBYsjnJd01MZaTXX0RAmL9AKCIYx/kPCONKHMfbI6aIAu0x/RduQCfb9DJ
Gc0rS4BvfkcEY7cvO+4ds/I=
=Dcfr
-----END PGP SIGNATURE-----
 
I

Irmen de Jong

Jeff said:
I wrote this program, and when I ran it the Python process grew to 200+
megs, and I was forced to kill it:
# Program 1: code at module level
import itertools
j = 0
for i in itertools.count():
j = j + str(i).count("1")
if j == i: print i

Weird. This program runs fine on my systems (win xp and mandrake-linux 10.0,
both usig python 2.3.4 ... using about 3.5 to 4mb memory.)
Perhaps it's a python 2.3.2 thing that was fixed in 2.3.3 or 2.3.4?

--Irmen.
 
J

jepler

Weird. This program runs fine on my systems (win xp and mandrake-linux 10.0,
both usig python 2.3.4 ... using about 3.5 to 4mb memory.)
Perhaps it's a python 2.3.2 thing that was fixed in 2.3.3 or 2.3.4?

Yep, I can't reproduce the behavior on 2.3.3 (Fedora Core 2). I guess
I should have checked.

The only thing I can think of that might be relevant is
A subtle change to the semantics of the built-in function
intern(): interned strings are no longer immortal. You must keep a
reference to the return value intern() around to get the benefit.
(if the numbers produced by str() were interned) but this change was in
2.3.2's news file, not 2.3.3.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBYxZQJd01MZaTXX0RAkgjAJ43mAbrOBGuHuzDCT25TQW+CpY9PwCeLLx+
RPmZJrvQp4lz1cVBYDZHjQU=
=QFcc
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top