【问题标题】:occur memory leak when import uuid导入uuid时发生内存泄漏
【发布时间】:2013-07-09 23:51:57
【问题描述】:

在导入 uuid 时发现内存泄漏。

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gc
>>> gc.set_debug(gc.DEBUG_LEAK)
>>> gc.collect()
0
>>> gc.garbage
[]
>>> import uuid
>>> gc.collect()
gc: collectable <tuple 026A2D30>
gc: collectable <_ctypes.PyCSimpleType 026DC190>
gc: collectable <getset_descriptor 026A4FA8>
gc: collectable <getset_descriptor 026A4FD0>
gc: collectable <tuple 026A5600>
gc: collectable <StgDict 026A61E0>
gc: collectable <tuple 026A84B0>
gc: collectable <StgDict 026AA7C8>
gc: collectable <_ctypes.PyCArrayType 026FAD30>
gc: collectable <getset_descriptor 026AD148>
gc: collectable <getset_descriptor 026AD170>
gc: collectable <tuple 026AC150>
gc: collectable <tuple 0269BA50>
gc: collectable <StgDict 026AA978>
gc: collectable <_ctypes.PyCFuncPtrType 026FB4D0>
gc: collectable <getset_descriptor 02699D50>
gc: collectable <getset_descriptor 02699E18>
gc: collectable <tuple 0269D180>
gc: collectable <tuple 0269BBF0>
gc: collectable <StgDict 026AAA50>
gc: collectable <_ctypes.PyCFuncPtrType 026FB6B8>
gc: collectable <getset_descriptor 026AD5D0>
gc: collectable <getset_descriptor 026AD5F8>
gc: collectable <tuple 0269D540>
25
**>>> gc.garbage**
[(<type '_ctypes._SimpleCData'>,), <class 'ctypes.c_longdouble'>, <attribute '__dict__' of 'c_longdouble' objects>, <attribute '__weakref__' of 'c_longdouble' objects>
ypes.c_longdouble'>, <type '_ctypes._SimpleCData'>, <type '_ctypes._CData'>, <type 'object'>), {'__dict__': <attribute '__dict__' of 'c_longdouble' objects>, '_type_':
le__': 'ctypes', '__weakref__': <attribute '__weakref__' of 'c_longdouble' objects>, '__doc__': None}, (<type '_ctypes.Array'>,), {'__module__': 'ctypes._endian', '__d
ibute '__dict__' of 'c_long_Array_3' objects>, '__weakref__': <attribute '__weakref__' of 'c_long_Array_3' objects>, '_length_': 3, '_type_': <class 'ctypes.c_long'>,
ne}, <class 'ctypes._endian.c_long_Array_3'>, <attribute '__dict__' of 'c_long_Array_3' objects>, <attribute '__weakref__' of 'c_long_Array_3' objects>, (<class 'ctype
ong_Array_3'>, <type '_ctypes.Array'>, <type '_ctypes._CData'>, <type 'object'>), (<type '_ctypes.PyCFuncPtr'>,), {'__module__': 'ctypes', '__dict__': <attribute '__di
ncPtr' objects>, '__weakref__': <attribute '__weakref__' of '_FuncPtr' objects>, '_flags_': 1, '__doc__': None, '_restype_': <class 'ctypes.c_long'>}, <class 'ctypes._
ttribute '__dict__' of '_FuncPtr' objects>, <attribute '__weakref__' of '_FuncPtr' objects>, (<class 'ctypes._FuncPtr'>, <type '_ctypes.PyCFuncPtr'>, <type '_ctypes._C
 'object'>), (<type '_ctypes.PyCFuncPtr'>,), {'__module__': 'ctypes', '__dict__': <attribute '__dict__' of '_FuncPtr' objects>, '__weakref__': <attribute '__weakref__'
' objects>, '_flags_': 1, '__doc__': None, '_restype_': <class 'ctypes.c_long'>}, <class 'ctypes._FuncPtr'>, <attribute '__dict__' of '_FuncPtr' objects>, <attribute '
of '_FuncPtr' objects>, (<class 'ctypes._FuncPtr'>, <type '_ctypes.PyCFuncPtr'>, <type '_ctypes._CData'>, <type 'object'>)]
**>>> len(gc.garbage)**
24
>>>

搜索相关资源后,我发现导入 ctypes 的问题相同。我在http://bugs.python.org/issue12142 发现了这个问题。

我想知道这是否也是我应该向 bugs.python.org 报告的错误。

【问题讨论】:

    标签: python memory garbage-collection memory-leaks uuid


    【解决方案1】:

    不是泄漏。设置gc.DEBUG_LEAK 会导致垃圾收集器将甚至可收集的对象附加到垃圾列表而不是释放它们。您可以看到收集器找到的所有内容都是免费的,因为所有消息都显示为collectable

    【讨论】:

    • 感谢您的回复。但是我有一个新的疑问,为什么 python 文档说 gc.DEBUG_SAVEALL 和 gc.DEBUG_LEAK 对于调试泄漏程序很有用。另外如果设置为DEBUG_UNCOLLECTABLE,最好能知道程序是否有泄漏。
    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2018-06-08
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多