You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python函数内部删除对象并清除所有引用?

Python中如何在函数内显式清理对象引用并释放内存

需求是编写逻辑移除对象的所有引用并删除对象,以此清理未使用的模拟对象。尝试了如下代码:

import numpy as np

class Unit:
    def __init__(self):
        # 模拟8GB数据
        self.data = np.random.randint(0, 1, 1000*1000*1000)
        self.HP = 5

def attack(unit):
    unit.HP -= 10
    if unit.HP <= 0:
        # 尝试移除所有引用
        del unit

unit = Unit()
attack(unit)
print(unit.data)  # 预期无法访问,但实际仍能输出
input('Press any key to exit...')

运行后输出:

[0 0 0 ... 0 0 0]
Press any key to exit...

而在函数外部直接调用del则能达到预期效果:

import numpy as np

class Unit:
    def __init__(self):
        # 模拟8GB数据
        self.data = np.random.randint(0, 1, 1000*1000*1000)
        self.HP = 5

def attack(unit):
    unit.HP -= 10
    if unit.HP <= 0:
        del unit

unit = Unit()
del unit
print(unit.data)  # 预期无法访问
input('Press any key to exit...')

运行后抛出错误:

print(unit.data)
          ^^^^
NameError: name 'unit' is not defined. Did you mean: 'Unit'?

问题根源

Python的变量本质是对象的引用,函数参数unit是全局unit的一个局部副本引用。在attack函数内执行del unit,只是删除了这个局部引用,全局作用域里的unit引用依然存在,对象的引用计数没有归零,所以垃圾回收器不会释放内存,外部自然还能访问unit.data。

可行解决方案

方案1:函数返回状态标记,由外部处理引用删除

让函数返回对象是否需要被销毁的标记,外部根据标记执行del:

import numpy as np

class Unit:
    def __init__(self):
        self.data = np.random.randint(0, 1, 1000*1000*1000)
        self.HP = 5

def attack(unit):
    unit.HP -= 10
    return unit.HP <= 0

unit = Unit()
if attack(unit):
    del unit

print(unit.data)  # 抛出NameError,符合预期
input('Press any key to exit...')

方案2:针对全局变量,在函数内声明global后删除

如果对象是全局作用域的变量,可以在函数内用global声明,直接操作全局引用:

import numpy as np

class Unit:
    def __init__(self):
        self.data = np.random.randint(0, 1, 1000*1000*1000)
        self.HP = 5

def attack():
    global unit
    unit.HP -= 10
    if unit.HP <= 0:
        del unit

unit = Unit()
attack()
print(unit.data)  # 抛出NameError,符合预期
input('Press any key to exit...')

注意:这种方式依赖全局变量,代码耦合度较高,不推荐在复杂场景使用。

方案3:用容器统一管理对象引用

将对象存入列表、字典等容器中,函数内操作容器移除对象引用,这样所有指向对象的引用都会被清理:

import numpy as np

class Unit:
    def __init__(self):
        self.data = np.random.randint(0, 1, 1000*1000*1000)
        self.HP = 5

def attack(unit_container):
    unit = unit_container[0]
    unit.HP -= 10
    if unit.HP <= 0:
        unit_container.clear()  # 移除容器内的引用

unit_container = [Unit()]
attack(unit_container)
print(unit_container[0].data)  # 抛出IndexError,符合预期
input('Press any key to exit...')

这种方式更灵活,适合管理多个对象的场景。

补充说明

Python的垃圾回收机制会自动处理引用计数归零的对象,显式执行del只是手动移除引用,加速内存释放。如果对象存在循环引用,还可以调用gc.collect()主动触发垃圾回收:

import gc
# 在移除所有引用后执行
gc.collect()

内容的提问来源于stack exchange,提问作者Seung Hyeon Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:43:21