本文介绍了Python中的手动垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以手动删除垃圾收集拒绝抛弃的对象,即使当我调用 gc.collect()时?在Python 3.0中工作

Is there any way to manually remove an object which the garbage collection refuses to get rid of even when I call gc.collect()? Working in Python 3.0

推荐答案

Per , gc.get_referrers(thatobject)会告诉你为什么 em>对象仍然活着(在 gc.collect())之后执行,以确保不希望的活性会持续存在)。之后,这是某种黑色艺术;-)。你会经常发现一些引用是列表(所以为什么是那个引用那个对象的列表?你可以 .remove 它在紧急模式下,但使正常代码声音更好......),甚至更经常地, dict s(其中许多可能是 __ dict __ s某些类别实例或其他 - 往往并非无足轻重以找出哪一个......再一次,暴力拆除有时是一种便捷的紧急解决方案,但永远不会持续很长时间 - 一个! - )。

Per the docs, gc.get_referrers(thatobject) will tell you why the object is still alive (do it right after a gc.collect() to make sure the undesired "liveness" is gonna be persistent). After that, it's somehow of a black art;-). You'll often find that some of the referrers are lists (so WHY is that list referring to thatobject? you can .remove it in an emergency mode, but making the normal code sound is better...), and, even more often, dicts (many of whose may be __dict__s of some class instance or other -- often not trivial to find out which one... again, brute-force removal is sometimes an expedient emergency solution, but never a sustainable long-range one!-).

这篇关于Python中的手动垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:02