如何在Python中分析内存使用情况?

如何在Python中分析内存使用情况?

最近,我开始对算法感兴趣,并开始探索算法,编写一个简单的实现,然后以各种方式对其进行优化。

我已经熟悉了用于分析运行时的标准Python模块(对于大多数情况,我发现IPython中的timeit魔术函数已经足够了),但我也对内存的使用感兴趣,因此我也可以探索这些权衡(例如,缓存一个表的先前计算的值与根据需要重新计算它们的成本)。是否有一个模块可以为我分析给定函数的内存使用情况?


料青山看我应如是
浏览 1221回答 3
3回答

倚天杖

这个问题已经在这里得到了答复:Python内存分析器基本上你做了类似的事情(引用自格皮):>>> from guppy import hpy; h=hpy()>>> h.heap()Partition of a set of 48477 objects. Total size = 3265516 bytes.&nbsp;Index&nbsp; Count&nbsp; &nbsp;%&nbsp; &nbsp; &nbsp;Size&nbsp; &nbsp;% Cumulative&nbsp; % Kind (class / dict of class)&nbsp; &nbsp; &nbsp;0&nbsp; 25773&nbsp; 53&nbsp; 1612820&nbsp; 49&nbsp; &nbsp;1612820&nbsp; 49 str&nbsp; &nbsp; &nbsp;1&nbsp; 11699&nbsp; 24&nbsp; &nbsp;483960&nbsp; 15&nbsp; &nbsp;2096780&nbsp; 64 tuple&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; 174&nbsp; &nbsp;0&nbsp; &nbsp;241584&nbsp; &nbsp;7&nbsp; &nbsp;2338364&nbsp; 72 dict of module&nbsp; &nbsp; &nbsp;3&nbsp; &nbsp;3478&nbsp; &nbsp;7&nbsp; &nbsp;222592&nbsp; &nbsp;7&nbsp; &nbsp;2560956&nbsp; 78 types.CodeType&nbsp; &nbsp; &nbsp;4&nbsp; &nbsp;3296&nbsp; &nbsp;7&nbsp; &nbsp;184576&nbsp; &nbsp;6&nbsp; &nbsp;2745532&nbsp; 84 function&nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; 401&nbsp; &nbsp;1&nbsp; &nbsp;175112&nbsp; &nbsp;5&nbsp; &nbsp;2920644&nbsp; 89 dict of class&nbsp; &nbsp; &nbsp;6&nbsp; &nbsp; 108&nbsp; &nbsp;0&nbsp; &nbsp; 81888&nbsp; &nbsp;3&nbsp; &nbsp;3002532&nbsp; 92 dict (no owner)&nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; 114&nbsp; &nbsp;0&nbsp; &nbsp; 79632&nbsp; &nbsp;2&nbsp; &nbsp;3082164&nbsp; 94 dict of type&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; 117&nbsp; &nbsp;0&nbsp; &nbsp; 51336&nbsp; &nbsp;2&nbsp; &nbsp;3133500&nbsp; 96 type&nbsp; &nbsp; &nbsp;9&nbsp; &nbsp; 667&nbsp; &nbsp;1&nbsp; &nbsp; 24012&nbsp; &nbsp;1&nbsp; &nbsp;3157512&nbsp; 97 __builtin__.wrapper_descriptor<76 more rows. Type e.g. '_.more' to view.>>>> h.iso(1,[],{})Partition of a set of 3 objects. Total size = 176 bytes.&nbsp;Index&nbsp; Count&nbsp; &nbsp;%&nbsp; &nbsp; &nbsp;Size&nbsp; &nbsp;% Cumulative&nbsp; % Kind (class / dict of class)&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; 1&nbsp; 33&nbsp; &nbsp; &nbsp; 136&nbsp; 77&nbsp; &nbsp; &nbsp; &nbsp;136&nbsp; 77 dict (no owner)&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; 1&nbsp; 33&nbsp; &nbsp; &nbsp; &nbsp;28&nbsp; 16&nbsp; &nbsp; &nbsp; &nbsp;164&nbsp; 93 list&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; 1&nbsp; 33&nbsp; &nbsp; &nbsp; &nbsp;12&nbsp; &nbsp;7&nbsp; &nbsp; &nbsp; &nbsp;176 100 int>>> x=[]>>> h.iso(x).sp&nbsp;0: h.Root.i0_modules['__main__'].__dict__['x']>>>&nbsp;

函数式编程

如果只想查看对象的内存使用情况,(对其他问题的回答)有一个模块名为打火机包含asizeof模块。用途如下:from&nbsp;pympler&nbsp;import&nbsp;asizeof asizeof.asizeof(my_object)不像sys.getsizeof,它适用于您自己创建的对象。.>>>&nbsp;asizeof.asizeof(tuple('bcd'))200>>>&nbsp;asizeof.asizeof({'foo':&nbsp;'bar',&nbsp;'baz':&nbsp;'bar'})400>>>&nbsp;asizeof.asizeof({})280>>>&nbsp;asizeof.asizeof({'foo':'bar'})360>>>&nbsp;asizeof.asizeof('foo')40>>>&nbsp;asizeof.asizeof(Bar())352>>>&nbsp;asizeof.asizeof(Bar().__dict__)280>>>&nbsp;help(asizeof.asizeof)Help&nbsp;on&nbsp;function&nbsp;asizeof&nbsp;in&nbsp;module&nbsp;pympler.asizeof:asizeof(*objs,&nbsp;**opts) &nbsp;&nbsp;&nbsp;&nbsp;Return&nbsp;the&nbsp;combined&nbsp;size&nbsp;in&nbsp;bytes&nbsp;of&nbsp;all&nbsp;objects&nbsp;passed&nbsp;as&nbsp;positional&nbsp;arguments.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python