- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
正如我们(或至少我)在 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:
- When the container is created.
- 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
的子类不必是不可变的。这意味着如果您想在 tuple
和 dict
之外扩展此机制,您需要一个通用的 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 的 GC 跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770515/
在我们对延迟敏感的应用程序中,我们有缓存数据(驻留在 TG 中)和在 YG 中消亡的短暂对象。我已经看到次要 GC 时间和主要 GC 时间有显着差异。我怀疑这与TG的尺寸相对较大有关。谁能解释 GC
我看到了多个建议运行 GC.Collect(GC.MaxGeneration) 的答案。 既然方法GC.Collect()会收集所有存在的分代,那么两者有什么区别吗? 也许如果只有两代而不是三代,GC
我们正在使用 UseParallelGC。 GC 日志看起来像 2016-06-09T19:38:17.362+0000:655312.397:[完整GC(人体工程学)[PSYoungGen:2291
我最近看到了两个非常好的和有教育意义的语言讲座: This first one由 Herb Sutter 撰写,介绍了 C++0x 的所有漂亮和酷炫的特性,为什么 C++ 的 future 似乎比以往
我们正在运行 gerrit 2.10.7,我们偶尔会遇到损坏的对象没有被 gerrit gc 修复的问题,即使 git gc 可以很好地修复它们。 另一方面,我读到 gerrit gc 会创建优化其他
我试图避免 Full GC(来自下面的 gc.log 示例)在生产中的 Tomcat 中运行 Grails 应用程序。关于如何更好地配置 GC 有什么建议吗? 14359.317:[完整 GC 143
我试图通过在析构函数中使用 console.WriteLine() 来确保释放某个类的实例,但输出从未出现。 我仔细搜索了任何挥之不去的引用资料以及事件订阅,但没有找到。只是为了我自己的理智,在我继续
之前看过一篇文章,说FGC影响时序,导致application出错结果。 代码示例如下: long start = System.currentTimeInMillis(); doSomething(
在 Java 中,我们可以使用 System.gc() 方法来建议 GC。今天我从this link开始了解C#中的GC.Collect()方法。 . 但我对解释有些不清楚。 第一行。 Forces
我理解 Python GC 有两种工作方式: 1) 基本引用计数 - 当“name”设置为“Tom”时,“John”下方的引用计数为零 name = "John" name = "Tom" (Refe
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
今天我们使用并发标记清除,具体如下: -XX:+UseConcMarkSweepGC 我看到一些文章推荐使用这种形式的附加参数: -XX:+UseConcMarkSweepGC -XX:+CMSInc
当我运行我的程序时,logcat 显示很多 GC Activity 喜欢 GC freed 10324 objects/ 510376 bytes in 103 ms GC freed 10324 o
2013-11-26T10:19:30.011+0800: [GC [ParNew: 2432484K->19997K(2696640K), 0.0378270 secs] 5560240K->315
在执行 GC 时,JVM 会遍历 Activity 对象,并清除未标记的对象。 根据: How to Tune Java Garbage Collection “Full GC的执行时间相对Minor
我有一个分布式缓存应用程序(内存绑定(bind),由于与集群中其他节点的交互而具有网络 I/O)在 JVM 1.7.0_51 中运行,带有 G1 垃圾收集器。这是 JVM 配置: -server -X
首先,我想让您知道,这是一个理论问题而不是实际问题,我只是好奇弱引用对象是如何被释放的。让我们快速记住 Java 中的弱引用是什么。粗略地说WeakReference意味着当没有指向“我”的强引用时,
这是运行大约 10 分钟后的输出。 Heap PSYoungGen total 7040K, used 0K [0x24060000, 0x247c0000, 0x26790000)
我正在运行一个应用程序,在 Weblogic 上使用 java 5 和 CMS 垃圾收集器。在垃圾收集日志中,我看到了消息日志,其中大部分消息我可以使用 Sun 的 Java HotSpot 虚拟机中
我有一个 ConcurrentMap> map = new ConcurrentHashMap>(); 并且希望当 SoftReference 的引用被 GC 时从映射中删除键/值对。 我该如何实现这
我是一名优秀的程序员,十分优秀!