我有一个 Python 客户端-服务器应用程序,它的服务器已经运行了大约 4 天...它的内存使用量(由/proc/PID/status -> VmSize 报告)已经增长到大约 660Mb,我当时想知道是否可能存在内存泄漏,所以我使用了 objgraph打印出它的内存使用情况(前 20 种对象类型):
58524 tuple
33270 dict
15483 function
9976 list
5396 set
2644 weakref
2489 builtin_function_or_method
2482 instancemethod
1898 OrderedSet
1751 _BindParamClause
1680 _generated_label
1485 Comparator
1398 type
1315 InstrumentedAttribute
1267 CustomColumn
1165 cell
1146 ScalarAttributeImpl
1146 ColumnProperty
1146 ColumnLoader
1075 wrapper_descriptor
我在那里没有看到任何特别“臭”的东西,我错过了什么吗?也许这只是因为 Python doesn't立即将未使用的内存释放给操作系统?
(这是一个CherryPy+SQLAlchemy应用)
请您参考如下方法:
要获取更有意义的数据,请让服务器运行几天,并每天提取一次此摘要。关键是看哪类元素一天天累积起来;这些对象类型最有可能导致内存泄漏!让我们知道您发现了什么。
