在 Python 中计算内存碎片 [英] Calculating memory fragmentation in Python
问题描述
我有一个长时间运行的进程,它不断地分配和释放对象.虽然对象正在被释放,但 RSS 内存使用量会随着时间的推移而上升.
I have a long running process that allocates and releases objects constantly. Although objects are being freed, the RSS mem usage goes up over time.
如何计算发生了多少碎片?一种可能性是计算 RSS/sum_of_allocations 并将其作为指标.即便如此,我该怎么做才能计算分母(sum_of_allocations).
How can I calculate how much fragmentation is happening? One possibility is to calculate RSS / sum_of_allocations and take that as an indicator. even then, how to do I calculate the denominator (sum_of_allocations).
推荐答案
查看垃圾收集器接口,gc.
Check out the Garbage Collector interface, gc.
http://docs.python.org/2/library/gc.html
您可以使用 gc.get_objects() 来检查正在跟踪的对象
You can inspect the objects are being tracked with gc.get_objects()
作为一般规则,原子类型的实例不会被跟踪,非原子类型(容器、用户定义的对象...)的实例会被跟踪."
"As a general rule, instances of atomic types aren’t tracked and instances of non-atomic types (containers, user-defined objects...) are."
还有 gc.garbage,它可以查找无法释放但无法访问的对象.
There is also gc.garbage, which finds objects that can't be freed but are unreachable.
这篇关于在 Python 中计算内存碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!