Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44669

Re: Trying to understand the memory occupation of big lists

Newsgroups comp.lang.python
Date 2013-05-03 08:20 -0700
References <e09e5cf1-4df5-4e77-9108-32bc3a1ae07d@googlegroups.com>
Message-ID <216083a7-fb00-471e-b123-af19ea6a7aef@googlegroups.com> (permalink)
Subject Re: Trying to understand the memory occupation of big lists
From Maarten <maarten.sneep@knmi.nl>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Trying to understand the memory occupation of big lists Michele Simionato <michele.simionato@gmail.com> - 2013-05-03 04:24 -0700
  Re: Trying to understand the memory occupation of big lists Dave Angel <davea@davea.name> - 2013-05-03 08:16 -0400
  Re: Trying to understand the memory occupation of big lists Maarten <maarten.sneep@knmi.nl> - 2013-05-03 08:20 -0700

csiph-web