Skip to content

profile result with unexpected big negative number #211

Description

@bingoabs

I want to profile the memory usage of objects with the codes below:

from memory_profiler import profile
NUM = 100000
class Person(object):
def __init__(self, name):
self.name = name
@profile
def run1():
ps = []
for i in range(NUM):
ps.append(Person(str(i)))
@profile
def run2():
r = []
for _ in range(NUM):
r.append({})
if __name__ == "__main__":
run1()
run2()

And I get the result:

$ python -m memory_profiler demo.py
Filename: demo.py
Line # Mem usage Increment Line Contents
================================================
12 10.8 MiB 10.8 MiB @profile
13 def run1():
14 10.8 MiB 0.0 MiB ps = []
15 53.1 MiB -744.4 MiB for i in range(NUM):
16 53.1 MiB -711.9 MiB ps.append(Person(str(i)))
Filename: demo.py
Line # Mem usage Increment Line Contents
================================================
18 14.8 MiB 14.8 MiB @profile
19 def run2():
20 14.8 MiB 0.0 MiB r = []
21 42.4 MiB -47.0 MiB for _ in range(NUM):
22 42.4 MiB -19.5 MiB r.append({})

The run1 line:15 and line:16 have a big negative number.

But if I change the global NUM to 10000, the result is total different:

$ python -m memory_profiler demo.py
Filename: demo.py
Line # Mem usage Increment Line Contents
================================================
12 10.7 MiB 10.7 MiB @profile
13 def run1():
14 10.7 MiB 0.0 MiB ps = []
15 15.1 MiB 0.3 MiB for i in range(NUM):
16 15.1 MiB 4.0 MiB ps.append(Person(str(i)))
Filename: demo.py
Line # Mem usage Increment Line Contents
================================================
18 11.6 MiB 11.6 MiB @profile
19 def run2():
20 11.6 MiB 0.0 MiB r = []
21 14.2 MiB 0.0 MiB for _ in range(NUM):
22 14.2 MiB 2.6 MiB r.append({})

How to explain the different between the two cases?
Should I consider the loop or something else, thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions