gpt4 book ai didi

python - 使用 heapy 追踪 Django 应用程序中的内存泄漏

转载 作者:太空狗 更新时间:2023-10-30 01:38:48 26 4
gpt4 key购买 nike

我在这里关注了如何使用 Django 设置 heapy 的优秀帖子:http://www.toofishes.net/blog/using-guppy-debug-django-memory-leaks/

我已经命令了 hp.setref(),现在过了一会儿我也通过 hp.heap() 获得了数据:

>>> hp.heap()
Partition of a set of 12075 objects. Total size = 1515496 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 4048 34 339656 22 339656 22 str
1 3112 26 269368 18 609024 40 tuple
2 171 1 169992 11 779016 51 dict (no owner)
3 1207 10 144440 10 923456 61 list
4 49 0 102040 7 1025496 68 dict of module
5 591 5 66984 4 1092480 72 unicode
6 498 4 59760 4 1152240 76 function
7 433 4 51960 3 1204200 79 types.CodeType
8 57 0 50480 3 1254680 83 type
9 36 0 31584 2 1286264 85 dict of class

但是现在呢?我应该从这个输出中理解什么?我应该如何开始追踪那些“str”和“tuple”对象所属的位置?

使用 get_rp,我得到以下输出:

>>> hp.heap().get_rp()
Reference Pattern by <[dict of] class>.
0: _ --- [-] 12000 (0xd1d340 | 0xd88b50 | 0xf63f00 | __builtin__.Struct | __...
1: a [-] 137 dict (no owner): 0x761c30*160, 0x7655d0*1491, 0x781640*9...
2: aa ---- [-] 45 dict of django.db.models.options.Options: 0xcf3110...
3: a3 [-] 45 django.db.models.options.Options: 0xcf3110, 0xf0bb10...
4: a4 ------ [-] 140 dict of django.db.models.related.RelatedObject: 0x10bec...
5: a5 [-] 140 django.db.models.related.RelatedObject: 0xf14450...
6: a6 -------- [-] 63 dict of django.db.models.fields.related.ForeignKey: 0x...
7: a7 [+] 63 django.db.models.fields.related.ForeignKey: 0xf0e690...
8: a5b ------- [-] 7 dict of django.db.models.fields.related.OneToOneField: ...
9: a5ba [+] 7 django.db.models.fields.related.OneToOneField: 0x15447...

现在是 Django 泄漏内存的正确假设吗?但是那些没有所有者的字典是什么?

最佳答案

我对 heapy 没有任何经验,但根据我的经验,Django(和大多数其他 Python 程序)不会泄漏内存,但它们也不会像某些人希望的那样干净利落地清理内存。

此外,Django 的设置会导致它出于诊断原因消耗内存。例如,设置 DEBUG=True 会导致它保留所有 SQL 查询,因此进程运行的时间越长,它使用的内存就越多。

更新:您的问题不在您的 Python 代码中。看看 heapy 给你的总结:那里代表的内存总大小是 1.5Mb!当 Python 程序真正泄漏时,最常见的原因是泄漏的 C 扩展。您是否有任何在 Django 进程下运行的 C 扩展?

关于python - 使用 heapy 追踪 Django 应用程序中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6972653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com