提交 c70f65c8 authored 作者: abergeron's avatar abergeron

Merge pull request #2714 from cangermueller/issue2652

Updates doc about internal memory management
...@@ -285,8 +285,8 @@ Let us prove my point using `memory\_profiler ...@@ -285,8 +285,8 @@ Let us prove my point using `memory\_profiler
<https://github.com/fabianp>`_ (the module's `github page <https://github.com/fabianp>`_ (the module's `github page
<https://github.com/fabianp/memory_profiler>`_). This add-on provides the <https://github.com/fabianp/memory_profiler>`_). This add-on provides the
decorator ``@profile`` that allows one to monitor one specific function decorator ``@profile`` that allows one to monitor one specific function
memory usage. It is extremely simple to use. Let us consider this small memory usage. It is extremely simple to use. Let us consider the following
program (it makes my point entirely): program:
:: ::
...@@ -295,12 +295,12 @@ program (it makes my point entirely): ...@@ -295,12 +295,12 @@ program (it makes my point entirely):
@profile @profile
def function(): def function():
x = range(1000000) # allocate a big list x = list(range(1000000)) # allocate a big list
y = copy.deepcopy(x) y = copy.deepcopy(x)
del x del x
return y return y
if __name__=="__main__": if __name__ == "__main__":
function() function()
invoking invoking
...@@ -317,30 +317,28 @@ prints, on a 64-bit computer ...@@ -317,30 +317,28 @@ prints, on a 64-bit computer
Line # Mem usage Increment Line Contents Line # Mem usage Increment Line Contents
================================================ ================================================
3 @profile 4 @profile
4 9.11 MB 0.00 MB def function(): 5 9.11 MB 0.00 MB def function():
5 40.05 MB 30.94 MB x=range(1000000) # allocate a big list 6 40.05 MB 30.94 MB x = list(range(1000000)) # allocate a big list
6 89.73 MB 49.68 MB y=copy.deepcopy(x) 7 89.73 MB 49.68 MB y = copy.deepcopy(x)
7 82.10 MB -7.63 MB del x 8 82.10 MB -7.63 MB del x
8 82.10 MB 0.00 MB return y 9 82.10 MB 0.00 MB return y
This small program creates a list with 1,000,000 ints (at 24 bytes each, This program creates a list of n=1,000,000 ints (n x 24 bytes = ~23 MB) and an
for ~24 million bytes) plus a list of references (at 8 bytes each, for ~8 additional list of references (n x 8 bytes = ~7.6 MB), which amounts to a total
million bytes), for about 30MB. It then deep-copies the object (which memory usage of ~31 MB. ``copy.deepcopy`` copies both lists, which allocates
allocates ~50MB, not sure why; a simple copy would allocate only 8MB of again ~50 MB (I am not sure where the additional overhead of 50 MB - 31 MB = 19
references, plus about 24MB for the objects themselves---so there's a large MB comes from). The interesting part is ``del x``: it deletes ``x``, but the
overhead here, maybe Python grew its heap preemptively). Freeing ``x`` with memory usage only decreases by 7.63 MB! This is because ``del`` only deletes the
``del`` frees the reference list, kills the associated objects, but lo!, reference list, not the actual integer values, which remain on the heap and
the amount of memory only goes down by the number of references, because cause a memory overhead of ~23 MB.
the list itself is not in a small objects' list, but on the heap, and the
dead small objects remain in the free list, and not returned to the This example allocates in total ~73 MB, which is more than *twice* the amount
interpreter's global heap. of memory needed to store a single list of ~31 MB. You can see that memory can
increase surprisingly if you are not careful!
In this example, we end up with *twice* the memory allocated, with 82MB,
while only one list necessitating about 30MB is returned. You can see why Note that you might get different results on a different platform or with a
it is easy to have memory just increase more or less surprisingly if we're different python version.
not careful.
Pickle Pickle
------ ------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论