GC - Possible to indefinitely prevent collection of generation-1 objects by calling gc.collect(0) often
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
The example below will die from memory exhaustion, despite nearly all objects being eligible for collection and despite garbage collection not being disabled. See inline comments for an explanation of what I think is happening:
import gc
from typing import Any
class Temp:
def __init__(self):
# Cyclic reference to prevent deletion by ref count dropping to 0.
self._x = self
# Allocate a bunch of memory so that the process will die if
# collection doesn't happen.
self.memory = bytearray(1024*1024*512)
# Set some low thresholds so that collection should occur often.
gc.set_threshold(100, 3, 3)
while True:
ref = Temp()
# Promote the new Temp() object to generation-1. I think this is also resetting
# the GCs view of how many objects were allocated at the "last collection". If
# that's true, it will effectively stop the GC from ever being invoked
# automatically, even though we're accumulating more and more objects,
# because `threshold0` will never be exceeded.
gc.collect(0)
del ref
# Temp() is now in generation-1 and eligible for collection, but gc.collect(1)
# is never triggered, because there are no automatically invoked GCs at all.
# Here we log the number of generation-1 objects, which continues to increase
# until the process dies.
print("Number of generation 1 objects: ", len(gc.get_objects(generation=1)))
Commenting out only the gc.collect(0) allows the Temp objects to be collected, and the process to run indefinitely. The fact that removing an explicit GC actually enables more GC to happen seems broken.
If what I think is happening is what's happening, then perhaps manually invoked GCs should not reset the GC's view on how many objects were allocated at the "last collection", since that would be what's effectively disabling the automatically triggered GCs (and hence the GCs at higher generations) from running.
CPython versions tested on:
3.11
Operating systems tested on:
macOS
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
未指定原始檔案或測試。首先在 CPython 3.11 上執行提供的重現程式,並追蹤 gc.collect(0) 的行為、自動收集閾值以及 generation-1 的增長。確認報告中的互動行為,並在驗證該行為後加入適當的回歸測試和修正,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100