fabianp/memory_profiler — pull request #59 by sleepin (2013-10-12 21:07:41+00:00)
Title: fixed unnessessary memory usage and line numbers (issue 7 "ignores last line")

Hi, Fabianp.
I liked using memory_profiler but I did find it to be badly flawed when working with the loops.

Here is the prove using guppy module.
```
Line # Mem usage Increment Line Contents
================================================
7 @profile
8 5.758 MiB 0.000 MiB def func():
9 6.094 MiB 0.336 MiB heap = guppy.hpy()
10 8.277 MiB 2.184 MiB heap.setref()
11 8.277 MiB 0.000 MiB a = []
12 14.367 MiB 6.090 MiB for x in range(10**5):
13 14.367 MiB 0.000 MiB a.append(x+1)
14 13.984 MiB -0.383 MiB del a
15 16.707 MiB 2.723 MiB print heap.heap()
Partition of a set of 200037 objects. Total size = 4021708 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 200004 100 3200064 80 3200064 80 float
1 5 0 819456 20 4019520 100 list
2 4 0 544 0 4020064 100 dict (no owner)
3 4 0 544 0 4020608 100 dict of guppy.etc.Glue.Interface
4 1 0 348 0 4020956 100 types.FrameType
5 5 0 180 0 4021136 100 types.MethodType
6 4 0 160 0 4021296 100 str
7 1 0 136 0 4021432 100 dict of guppy.etc.Glue.Owner
8 3 0 120 0 4021552 100 __builtin__.weakref
9 4 0 112 0 4021664 100 guppy.etc.Glue.Interface
<2 more rows. Type e.g. '_.more' to view.>
```
We create 200004 floats and some big fat list object for no good reason! Lets take a look at the fix:

```
Line # Mem usage Increment Line Contents
================================================
3 6.031 MiB 0.000 MiB @profile
4 def func():
5 6.371 MiB 0.340 MiB heap = guppy.hpy()
6 8.508 MiB 2.137 MiB heap.setref()
7 8.508 MiB 0.000 MiB a = []
8 10.676 MiB 2.168 MiB for x in range(10**5):
9 11.059 MiB 0.383 MiB a.append(x+1)
10 10.293 MiB -0.766 MiB del a
11 9.027 MiB -1.266 MiB print heap.heap()

Partition of a set of 72 objects. Total size = 3828 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 42 58 1680 44 1680 44 __builtin__.weakref
1 4 6 544 14 2224 58 dict (no owner)
2 4 6 544 14 2768 72 dict of guppy.etc.Glue.Interface
3 1 1 348 9 3116 81 types.FrameType
4 5 7 180 5 3296 86 types.MethodType
5 4 6 160 4 3456 90 str
6 1 1 136 4 3592 94 dict of guppy.etc.Glue.Owner
7 4 6 112 3 3704 97 guppy.etc.Glue.Interface
8 5 7 80 2 3784 99 float
9 1 1 32 1 3816 100 guppy.etc.Glue.Owner
10 1 1 12 0 3828 100 int
```

Looks much better to me and memory reads are more honest.
I also had tidied up code a little bit and fixed issue 8 "strange behavior for growing dictionaries/tuples"

Take a look:
```

Line # Mem usage Increment Line Contents
================================================
3 6.008 MiB 0.000 MiB @profile
4 def my_func_dict():
5 6.008 MiB 0.000 MiB a = {}
6 6.680 MiB 0.672 MiB for i in range(10000):
7 6.680 MiB 0.000 MiB a[i] = i + 1
```

Also, I have no idea why you need self.functions list in LineProfiler, so I deleted it.