从python 2迁移后,python 3应用程序内存泄漏

2024-09-27 00:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我将一个Python2应用程序迁移到Python3。如果我使用Python3解释器,我会注意到内存泄漏。如果我将解释器改回Python2,它可以正常工作。我使用futurize工具使代码与Python2和Python3兼容

  1. 我使用tracemalloc查看哪行代码分配了更多内存
  2. gc模块获取垃圾收集统计信息等

但是我找不到任何有用的东西。py3中是否有任何与垃圾收集相关的更改?应用程序是一个服务器,它接受telnet连接并处理用户输入。每秒都有数百个请求。在每次迭代中,对象的数量不断增加

                    objects = gc.get_objects()
                    map_dict = defaultdict()
                    for o in objects:
                        count = map_dict.get(type(o).__name__, 0)
                        map_dict[type(o).__name__] = count + 1

                    LOG.info('=======>')
                    for k, v in map_dict.items():
                        if v > 1000:
                            LOG.info(k + " : " + str(v))
                    LOG.info(len(gc.get_objects()))
                    LOG.info(gc.get_stats())
                    LOG.info('<=======')


tuple : 6004
list : 4130
frame : 6196
builtin_function_or_method : 5973
dict : 8931
wrapper_descriptor : 1324
method_descriptor : 1173
getset_descriptor : 1667
weakref : 2996
function : 9918
type : 1612
set : 1097
method : 2143

62654
[{'collections': 65813, 'collected': 894, 'uncollectable': 0}, 
 {'collections': 5982, 'collected': 1117, 'uncollectable': 0}, 
 {'collections': 543, 'collected': 460, 'uncollectable': 0}]

Tags: infologmapgetobjectstypemethodcollections

热门问题