Python的内存泄漏及gc模块的使用分析

作者:shichen2014 时间:2023-09-17 18:07:30 

一般来说在 Python 中,为了解决内存泄漏问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收。
由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为自己从此过上了好日子,不必再受内存泄漏的骚扰了。但如果仔细查看一下Python文档对 __del__() 函数的描述,就知道这种好日子里也是有阴云的。下面摘抄一点文档内容如下:

Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in sys.exc_traceback keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in sys.last_traceback keeps the stack frame alive).

可见,有 __del__() 函数的对象间的循环引用是导致内存泄漏的主凶。
另外需要说明:对没有 __del__() 函数的 Python 对象间的循环引用,是可以被自动垃圾回收掉的。

如何知道一个对象是否内存泄漏了呢?

方法一、当你认为一个对象应该被销毁时(即引用计数为 0),可以通过 sys.getrefcount(obj) 来获取对象的引用计数,并根据返回值是否为 0 来判断是否内存泄漏。如果返回的引用计数不为 0,说明在此刻对象 obj 是不能被垃圾回收器回收掉的。

方法二、也可以通过 Python 扩展模块 gc 来查看不能回收的对象的详细信息。


首先,来看一段正常的测试代码:


#--------------- code begin --------------
# -*- coding: utf-8 -*-
import gc
import sys

class CGcLeak(object):
 def __init__(self):
   self._text = '#'*10

def __del__(self):
   pass

def make_circle_ref():
 _gcleak = CGcLeak()
#  _gcleak._self = _gcleak # test_code_1
 print '_gcleak ref count0:%d' % sys.getrefcount(_gcleak)
 del _gcleak
 try:
   print '_gcleak ref count1:%d' % sys.getrefcount(_gcleak)
 except UnboundLocalError:
   print '_gcleak is invalid!'

def test_gcleak():
 # Enable automatic garbage collection.
 gc.enable()
 # Set the garbage collection debugging flags.
 gc.set_debug(gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | /
   gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS)

print 'begin leak test...'
 make_circle_ref()

print 'begin collect...'
 _unreachable = gc.collect()
 print 'unreachable object num:%d' % _unreachable
 print 'garbage object num:%d' % len(gc.garbage)

if __name__ == '__main__':
 test_gcleak()

在 test_gcleak() 中,设置垃圾回收器调试标志后,再用 collect() 进行垃圾回收,最后打印出该次垃圾回收发现的不可达的垃圾对象数和整个解释器中的垃圾对象数。

gc.garbage 是一个 list 对象,列表项是垃圾收集器发现的不可达(即是垃圾对象)、但又不能释放(即不能回收)的对象。文档描述为:A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects).
通常,gc.garbage 中的对象是引用环中的对象。因为 Python 不知道按照什么样的安全次序来调用环中对象的 __del__() 函数,导致对象始终存活在 gc.garbage 中,造成内存泄漏。如果知道一个安全的次序,那么就打破引用环,再执行 del gc.garbage[:] ,以清空垃圾对象列表。

上段代码输出为(#后字符串为笔者所加注释):


#-----------------------------------------
begin leak test...
# 变量 _gcleak 的引用计数为 2.
_gcleak ref count0:2
# _gcleak 变为不可达(unreachable)的非法变量.
_gcleak is invalid!
# 开始垃圾回收
begin collect...
# 本次垃圾回收发现的不可达的垃圾对象数为 0.
unreachable object num:0
# 整个解释器中的垃圾对象数为 0.
garbage object num:0
#-----------------------------------------

由此可见 _gcleak 对象的引用计数是正确的,也没有任何对象发生内存泄漏。

如果不注释掉 make_circle_ref() 中的 test_code_1 语句:


_gcleak._self = _gcleak

也就是让 _gcleak 形成一个自己对自己的循环引用。再运行上述代码,输出结果就变成:


#-----------------------------------------
begin leak test...
_gcleak ref count0:3
_gcleak is invalid!
begin collect...
# 发现可以回收的垃圾对象: 地址为 012AA090,类型为 CGcLeak.
gc: uncollectable <CGcLeak 012AA090>
gc: uncollectable <dict 012AC1E0>
unreachable object num:2
#!! 不能回收的垃圾对象数为 1,导致内存泄漏!
garbage object num:1
#-----------------------------------------

可见 <CGcLeak 012AA090> 对象发生了内存泄漏!!而多出的 dict 垃圾就是泄漏的 _gcleak 对象的字典,打印出字典信息为:


{'_self': <__main__.CGcLeak object at 0x012AA090>, '_text': '##########'}

除了对自己的循环引用,多个对象间的循环引用也会导致内存泄漏。简单举例如下:


#--------------- code begin --------------

class CGcLeakA(object):
 def __init__(self):
   self._text = '#'*10

def __del__(self):
   pass

class CGcLeakB(object):
 def __init__(self):
   self._text = '*'*10

def __del__(self):
   pass

def make_circle_ref():
 _a = CGcLeakA()
 _b = CGcLeakB()
 _a._b = _b # test_code_2
 _b._a = _a # test_code_3
 print 'ref count0:a=%d b=%d' % /
   (sys.getrefcount(_a), sys.getrefcount(_b))
#  _b._a = None  # test_code_4
 del _a
 del _b
 try:
   print 'ref count1:a=%d' % sys.getrefcount(_a)
 except UnboundLocalError:
   print '_a is invalid!'
 try:
   print 'ref count2:b=%d' % sys.getrefcount(_b)
 except UnboundLocalError:
   print '_b is invalid!'

#--------------- code end ----------------

这次测试后输出结果为:


#-----------------------------------------
begin leak test...
ref count0:a=3 b=3
_a is invalid!
_b is invalid!
begin collect...
gc: uncollectable <CGcLeakA 012AA110>
gc: uncollectable <CGcLeakB 012AA0B0>
gc: uncollectable <dict 012AC1E0>
gc: uncollectable <dict 012AC0C0>
unreachable object num:4
garbage object num:2
#-----------------------------------------

可见 _a,_b 对象都发生了内存泄漏。因为二者是循环引用,垃圾回收器不知道该如何回收,也就是不知道该首先调用那个对象的 __del__() 函数。

采用以下任一方法,打破环状引用,就可以避免内存泄漏:

1.注释掉 make_circle_ref() 中的 test_code_2 语句;
2.注释掉 make_circle_ref() 中的 test_code_3 语句;
3.取消对 make_circle_ref() 中的 test_code_4 语句的注释。

相应输出结果变为:


#-----------------------------------------
begin leak test...
ref count0:a=2 b=3 # 注:此处输出结果视情况变化.
_a is invalid!
_b is invalid!
begin collect...
unreachable object num:0
garbage object num:0
#-----------------------------------------

结论:Python 的 gc 有比较强的功能,比如设置 gc.set_debug(gc.DEBUG_LEAK) 就可以进行循环引用导致的内存泄露的检查。如果在开发时进行内存泄露检查;在发布时能够确保不会内存泄露,那么就可以延长 Python 的垃圾回收时间间隔、甚至主动关闭垃圾回收机制,从而提高运行效率。

标签:Python,内存,gc模块
0
投稿

猜你喜欢

  • 基于python实现的抓取腾讯视频所有电影的爬虫

    2023-02-20 22:01:39
  • 基于进程内通讯的python聊天室实现方法

    2021-01-24 03:50:53
  • Python基于datetime或time模块分别获取当前时间戳的方法实例

    2021-01-19 22:53:18
  • golang 随机数的两种方式

    2024-04-28 10:46:44
  • Go语言入门教程之Arrays、Slices、Maps、Range操作简明总结

    2024-05-09 14:56:40
  • python 生成目录树及显示文件大小的代码

    2022-10-24 18:30:27
  • mysql数据库删除重复数据只保留一条方法实例

    2024-01-28 06:17:49
  • 使用Python保存网页上的图片或者保存页面为截图

    2022-04-08 10:45:19
  • python语言是免费还是收费的?

    2021-12-30 10:04:48
  • FastApi+Vue+LayUI实现前后端分离的示例代码

    2024-04-30 10:22:48
  • python库sklearn常用操作

    2022-06-24 13:44:33
  • Python 做曲线拟合和求积分的方法

    2021-03-03 01:46:07
  • Vue使用Element-UI生成并展示表头序号的方法

    2024-06-05 10:04:27
  • numpy中三维数组中加入元素后的位置详解

    2021-02-28 10:25:14
  • 天气预报调用代码

    2008-11-18 15:59:00
  • Python下使用Scrapy爬取网页内容的实例

    2022-05-29 13:43:24
  • 在Python中使用gRPC的方法示例

    2021-02-02 16:20:21
  • MySQL单表查询常见操作实例总结

    2024-01-20 05:47:17
  • django admin 根据choice字段选择的不同来显示不同的页面方式

    2022-04-26 06:39:10
  • jQuery 1.4新特性及其变化(上)

    2010-01-18 16:33:00
  • asp之家 网络编程 m.aspxhome.com