Trying to understand the memory occupation of big lists

Maarten maarten.sneep at knmi.nl
Fri May 3 11:20:27 EDT 2013


I made a few changes:

import gc
from memory_profiler import profile

@profile
def test1():
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    a = [0] * 1024**2
    del a
    gc.collect()  # nothing change if I comment this


@profile
def test2():
    for i in range(10):
        a = [0] * 1024**2
        del a
    del i
    gc.collect()  # nothing change if I comment this


test1()
test2() 

# end of code

Output:

Filename: profile.py

Line #    Mem usage    Increment   Line Contents
================================================
     5                             @profile
     6     8.688 MB     0.000 MB   def test1():
     7    16.691 MB     8.004 MB       a = [0] * 1024**2
     8     8.688 MB    -8.004 MB       del a
     9    16.680 MB     7.992 MB       a = [0] * 1024**2
    10    16.680 MB     0.000 MB       del a
    11    16.680 MB     0.000 MB       a = [0] * 1024**2
    12    16.680 MB     0.000 MB       del a
    13    16.680 MB     0.000 MB       a = [0] * 1024**2
    14    16.680 MB     0.000 MB       del a
    15    16.680 MB     0.000 MB       a = [0] * 1024**2
    16    16.680 MB     0.000 MB       del a
    17    16.680 MB     0.000 MB       a = [0] * 1024**2
    18    16.680 MB     0.000 MB       del a
    19    16.680 MB     0.000 MB       a = [0] * 1024**2
    20    16.680 MB     0.000 MB       del a
    21    16.680 MB     0.000 MB       a = [0] * 1024**2
    22    16.680 MB     0.000 MB       del a
    23    16.680 MB     0.000 MB       a = [0] * 1024**2
    24    16.680 MB     0.000 MB       del a
    25    16.680 MB     0.000 MB       a = [0] * 1024**2
    26    16.680 MB     0.000 MB       del a
    27    16.680 MB     0.000 MB       gc.collect()  # nothing change if I comment this


Filename: profile.py

Line #    Mem usage    Increment   Line Contents
================================================
    30                             @profile
    31    16.691 MB     0.000 MB   def test2():
    32    16.691 MB     0.000 MB       for i in range(10):
    33    16.691 MB     0.000 MB           a = [0] * 1024**2
    34    16.691 MB     0.000 MB           del a
    35    16.691 MB     0.000 MB       del i
    36    16.691 MB     0.000 MB       gc.collect()  # nothing change if I comment this

If I make the two functions identical, the behave the same.

Maarten



More information about the Python-list mailing list