python - 为什么命名元组总是被 python 的 GC 跟踪?

标签 python garbage-collection

正如我们(或至少我)在 this answer 中了解到的那样仅包含不可变值的简单元组不会被 python 的垃圾收集器跟踪,一旦它发现它们永远不会涉及引用循环:

>>> import gc
>>> x = (1, 2)
>>> gc.is_tracked(x)
True
>>> gc.collect()
0
>>> gc.is_tracked(x)
False

为什么 namedtuple 不是这种情况? s,它们是来自具有命名字段的集合模块的元组的子类?

>>> import gc
>>> from collections import namedtuple
>>> foo = namedtuple('foo', ['x', 'y'])
>>> x = foo(1, 2)
>>> gc.is_tracked(x)
True
>>> gc.collect()
0
>>> gc.is_tracked(x)
True

他们的实现中是否有一些固有的东西可以防止这种情况发生,或者只是被忽视了?

最佳答案

我能找到的关于此的唯一注释是在 Python 源代码的 gcmodule.c 文件中:

NOTE: about untracking of mutable objects. Certain types of container cannot participate in a reference cycle, and so do not need to be tracked by the garbage collector. Untracking these objects reduces the cost of garbage collections. However, determining which objects may be untracked is not free, and the costs must be weighed against the benefits for garbage collection.

There are two possible strategies for when to untrack a container:

  1. When the container is created.
  2. When the container is examined by the garbage collector.

Tuples containing only immutable objects (integers, strings etc, and recursively, tuples of immutable objects) do not need to be tracked. The interpreter creates a large number of tuples, many of which will not survive until garbage collection. It is therefore not worthwhile to untrack eligible tuples at creation time.

Instead, all tuples except the empty tuple are tracked when created. During garbage collection it is determined whether any surviving tuples can be untracked. A tuple can be untracked if all of its contents are already not tracked. Tuples are examined for untracking in all garbage collection cycles. It may take more than one cycle to untrack a tuple.

Dictionaries containing only immutable objects also do not need to be tracked. Dictionaries are untracked when created. If a tracked item is inserted into a dictionary (either as a key or value), the dictionary becomes tracked. During a full garbage collection (all generations), the collector will untrack any dictionaries whose contents are not tracked.

The module provides the python function is_tracked(obj), which returns the current tracking status of the object. Subsequent garbage collections may change the tracking status of the object. Untracking of certain containers was introduced in issue #4688, and the algorithm was refined in response to issue #14775.

(查看链接的问题以查看为允许取消跟踪而引入的真实代码)

这条评论有点模棱两可,但它没有说明选择要“取消跟踪”的对象的算法适用于通用容器。这意味着代码只检查 tuple(和 dict),而不检查它们的子类。

您可以在文件的代码中看到这一点:

/* Try to untrack all currently tracked dictionaries */
static void
untrack_dicts(PyGC_Head *head)
{
    PyGC_Head *next, *gc = head->gc.gc_next;
    while (gc != head) {
        PyObject *op = FROM_GC(gc);
        next = gc->gc.gc_next;
        if (PyDict_CheckExact(op))
            _PyDict_MaybeUntrack(op);
        gc = next;
    }
}

注意对 PyDict_CheckExact 的调用,并且:

static void
move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
{
    PyGC_Head *gc = young->gc.gc_next;

  /* omissis */
            if (PyTuple_CheckExact(op)) {
                _PyTuple_MaybeUntrack(op);
            }

请注意对 PyTuple_CheckExact 的调用。

另请注意,tuple 的子类不必是不可变的。这意味着如果您想在 tupledict 之外扩展此机制,您需要一个通用的 is_immutable 函数。由于 Python 的动态性(例如,类的方法可能会在运行时更改,而这对于 tuple 来说是不可能的,因为它是一个内置类型)。因此,开发人员选择坚持少数特殊情况,仅使用一些众所周知的内置插件。


这就是说,我相信它们也可以特例namedtuple,因为它们是非常简单的类。会有一些问题,例如,当您调用 namedtuple 时,您正在创建一个 类,因此 GC 应该检查一个子类。 这可能是以下代码的问题:

class MyTuple(namedtuple('A', 'a b')):
    # whatever code you want
    pass

因为 MyTuple 类需要是不可变的,所以 GC 应该检查该类是 namedtuple 的直接子类 为了安全起见。不过,我很确定这种情况有解决方法。

他们可能没有,因为 namedtuple 是标准库的一部分,而不是 python 核心。也许开发人员不想让核心依赖于标准库的模块。

所以,回答你的问题:

  • 不,在他们的实现中没有任何东西可以内在地阻止对 namedtuple
  • 的取消跟踪
  • 不,我相信他们没有“只是忽略”了这一点。然而,只有 python 开发人员才能明确回答为什么他们选择不包括它们。我的猜测是,他们认为这不会为更改提供足够大的好处,并且他们不想让核心依赖于标准库。

关于python - 为什么命名元组总是被 python 的 GC 跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770515/

相关文章:

Python numpy 数组添加/更新/删除来自其他数组的值的行

python - Django:css 引用静态文件中的媒体(django dev/1.3/static 文件)

python - 从查询集中获取查询集

python - Python 的 Tkinter 字段为空白

c++ - 如何缓存 Ruby VALUE 值以供以后重用?

java - 为什么会有Full GC?

garbage-collection - 为什么 Rust 需要返回静态大小?

python - 是否有可能*实时*修改 Python 代码(如 Lisp 或 Erlang)

c# - 在对象被释放之前如何执行操作?

.net - 是否可以使用单声道检测当前的垃圾收集器实现?