显示了python程序中内存泄漏的主要嫌疑犯。
mem_top的Python项目详细描述
用法:
pip install mem_top from mem_top import mem_top # From time to time: logging.debug(mem_top()) # Or just print(). # Notice which counters keep increasing over time - they are the suspects.
计数器:
- refs-从这个对象到其他对象的直接引用的数目,如dict的键和值
- bytes—此对象的大小(字节)
- Types—垃圾回收后仍保留在内存中的此类对象数
现实生活的例子:
refs: 144997 <type 'collections.defaultdict'> defaultdict(<type 'collections.deque'>, {<GearmanJobRequest task='...', unique='. 144996 <type 'dict'> {'.:..............:.......': <GearmanJobRequest task='..................', unique='................. 18948 <type 'dict'> {... 1578 <type 'dict'> {... 968 <type 'dict'> {... 968 <type 'dict'> {... 968 <type 'dict'> {... 767 <type 'list'> [... 726 <type 'dict'> {... 608 <type 'dict'> {... types: 292499 <type 'dict'> 217912 <type 'collections.deque'> 72702 <class 'gearman.job.GearmanJob'> 72702 <class 'gearman.job.GearmanJobRequest'> 12340 <type '... 3103 <type '... 1112 <type '... 855 <type '... 767 <type '... 532 <type '...
- 注意到6GB内存泄漏并计数
- 添加了“mem_top”并让它运行一段时间。
- 当得到上述结果时,很明显谁在泄漏: gearman的python客户端一直在增加计数器。
- 找到了它的已知错误-https://github.com/Yelp/python-gearman/issues/10 泄漏deques的defaultdict和gearmanjobrequest-s的dict, 就像“mem_top”显示的那样
- 取代了“python gearman”-说来话长:PyPI的2.0.2过时了,github的2.0.X坏了,等等
- “mem_top”确认泄漏现已完全关闭。
更新:
- 传递例如verbose_types=[dict, list]以获取它们在verbose_file_name中按repr长度排序的值。
- 在顶部添加了“字节”
另请参见:
配置默认值:
mem_top( limit=10, width=100, sep='\n', refs_format='{num}\t{type} {obj}', bytes_format='{num}\t {obj}', types_format='{num}\t {obj}', verbose_types=None, verbose_file_name='/tmp/mem_top', )