tiny script has memory leak
Terry Reedy
tjreedy at udel.edu
Thu May 17 09:42:00 EDT 2012
On 5/17/2012 5:50 AM, Alain Ketterlin wrote:
> gry<georgeryoung at gmail.com> writes:
>
>> 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()
That would have to be f.write(str(random.randint(0, sys.maxint))+end)
where above end would be '\n'.
> instead of print>> f (which I think is deprecated).
In the sense that in Py3, print is a function with a file parameter:
print(random.randint(0, sys.maxint), file=f)
The idiosyncratic ugliness of >>file was one reason for the change.
Adding the option to specify separator and terminator was another.
--
Terry Jan Reedy
More information about the Python-list
mailing list