tiny script has memory leak

G

gry

sys.version --> '2.6 (r26:66714, Feb 21 2009, 02:16:04) \n[GCC 4.3.2
[gcc-4_3-branch revision 141291]]
I thought this script would be very lean and fast, but with a large
value for n (like 150000), it uses 26G of virtural memory, and things
start to crumble.

#!/usr/bin/env python
'''write a file of random integers. args are: file-name how-many'''
import sys, random

f = open(sys.argv[1], 'w')
n = int(sys.argv[2])
for i in xrange(n):
print >>f, random.randint(0, sys.maxint)
f.close()

What's using so much memory?
What would be a better way to do this? (aside from checking arg
values and types, I know...)
 
I

Iain King

sys.version --> '2.6 (r26:66714, Feb 21 2009, 02:16:04) \n[GCC 4.3.2
[gcc-4_3-branch revision 141291]]
I thought this script would be very lean and fast, but with a large
value for n (like 150000), it uses 26G of virtural memory, and things
start to crumble.

#!/usr/bin/env python
'''write a file of random integers. args are: file-name how-many'''
import sys, random

f = open(sys.argv[1], 'w')
n = int(sys.argv[2])
for i in xrange(n):
print >>f, random.randint(0, sys.maxint)
f.close()

What's using so much memory?
What would be a better way to do this? (aside from checking arg
values and types, I know...)

Ran OK for me, python 2.4.1 on Windows 7

Iain
 
A

Alain Ketterlin

gry said:
sys.version --> '2.6 (r26:66714, Feb 21 2009, 02:16:04) \n[GCC 4.3.2
[gcc-4_3-branch revision 141291]]
I thought this script would be very lean and fast, but with a large
value for n (like 150000), it uses 26G of virtural memory, and things
start to crumble.

#!/usr/bin/env python
'''write a file of random integers. args are: file-name how-many'''
import sys, random

f = open(sys.argv[1], 'w')
n = int(sys.argv[2])
for i in xrange(n):
print >>f, random.randint(0, sys.maxint)
f.close()

sys.version is '2.6.6 (r266:84292, Sep 15 2010, 16:22:56) \n[GCC 4.4.5]'
here, and your script works like a charm. BTW, I would use f.write()
instead of print >> f (which I think is deprecated).

-- Alain.
 

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

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top