Function Memory Usage
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Nov 1 09:04:29 EDT 2008
On Sat, 01 Nov 2008 01:43:44 -0400, Terry Reedy wrote:
> Paulo J. Matos wrote:
>> Hi all,
>>
>> What's the best way to know the amount of memory allocated by a
>> function
>
> What do you count?
>
> def zeros(n): return [0]*n
>
> how much memory is that?
I assume you don't include the memory required by the function itself.
Integers take 20 bytes, but rounded to the nearest multiple of 8 bytes,
so 24 bytes. However, we can assume that the 0 int is cached, so we only
need 24 bytes for all n of them.
Lists have a fixed overhead of 28 bytes, plus 4*n for the items, again
rounded to the nearest multiple of 8 bytes.
Add them together, and you have the memory usage of the list returned.
--
Steven
More information about the Python-list
mailing list