I recently met this article on python memory allocation.
This page describes python memory usage, and there is an example showing the depth of a list of integers. I did the test myself in Python 2.7
Line # Mem usage Increment Line Contents ================================================ 4 28.051 MiB 0.000 MiB @profile 5 def function(): 6 59.098 MiB 31.047 MiB x = list(range(1000000)) # allocate a big list 7 107.273 MiB 48.176 MiB y = copy.deepcopy(x) 8 99.641 MiB -7.633 MiB del x 9 99.641 MiB 0.000 MiB return y
therefore delete x directly removes only x and all references to integer to x right?
Doing this also did not help (so what is the difference between del x and del x [:]?):
Line # Mem usage Increment Line Contents ================================================ 4 28.047 MiB 0.000 MiB @profile 5 def function(): 6 59.094 MiB 31.047 MiB x = list(range(1000000)) # allocate a big list 7 107.270 MiB 48.176 MiB y = copy.deepcopy(x) 8 99.637 MiB -7.633 MiB del x[:] 9 99.637 MiB 0.000 MiB return y
And unlike deepcopy, if I use a copy, after deleting it seems that the memory is restored to its previous state, when x is newly created
Line # Mem usage Increment Line Contents ================================================ 4 28.039 MiB 0.000 MiB @profile 5 def function(): 6 59.090 MiB 31.051 MiB x = list(range(1000000)) # allocate a big list 7 66.895 MiB 7.805 MiB y = copy.copy(x) 8 59.262 MiB -7.633 MiB del x[:] 9 59.262 MiB 0.000 MiB return y
For dict:
Line # Mem usage Increment Line Contents ================================================ 4 28.051 MiB 0.000 MiB @profile 5 def function(): 6 100.523 MiB 72.473 MiB x = dict((e, e) for e in xrange(1000000)) 7 183.398 MiB 82.875 MiB y = copy.deepcopy(x) 8 135.395 MiB -48.004 MiB del x 9 135.395 MiB 0.000 MiB return y
And for the list of lists (compare with an integer, I suppose that del x or del x [:] only removes this huge list of arrays on the heap?):
Line # Mem usage Increment Line Contents ================================================ 4 28.043 MiB 0.000 MiB @profile 5 def function(): 6 107.691 MiB 79.648 MiB x = [[] for _ in xrange(1000000)] 7 222.312 MiB 114.621 MiB y = copy.deepcopy(x) 8 214.680 MiB -7.633 MiB del x[:] 9 214.680 MiB 0.000 MiB return y
Therefore, I want to ask:
- So, if there is no way to get back the memory occupied by integers? Is the whole also an object? Why is memory not being released at all? Just the whole cannot be stated? Or swim and string? References to objects also?
- Why is there -7 MB for memory? Is it because a list implemented as a list of arrays is freed from the heap?
- whether it is a list or a dict, del x can only free the data structure (what I mean is that the structure of the array list or the dict structure), but integers, references to objects can be marked as free, but did not return to system?
And how am I or if there is a way to free all underscores in x in this example?
Line # Mem usage Increment Line Contents ================================================ 4 28.047 MiB 0.000 MiB @profile 5 def function(): 6 248.008 MiB 219.961 MiB x = [list(range(10)) for _ in xrange(1000000)] 7 502.195 MiB 254.188 MiB y = copy.deepcopy(x) 8 494.562 MiB -7.633 MiB del x[:] 9 494.562 MiB 0.000 MiB return y