python / python/cpython

C Unpickler memory leak when unpickling big geojsons

未關閉
#121,569 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

I noticed that the pickle.load method leaves more memory allocated than just the size of the returned object when unpickling geojson dictionaries.

I initially noticed the leak in my docker container running on x86. Here is a code snippet that reproduced the problem for me on M1 MacOS Sonoma (python 3.11.9).

import io
import pickle

import psutil

process = psutil.Process()

to_pickle = {
    "type": "FeatureCollection",
    "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"},
    "features": [
        {
            "type": "Feature",
            "id": f"id{idx}",
            "properties": {
                "str_prop": f"str_var{idx}",
                "int_prop": idx,
                "bool_prop": bool(idx % 2),
                "none_prop": None,
                "float_prop": float(idx),
            },
        }
        for idx in range(9000)
    ],
}
b = pickle.dumps(to_pickle)
del to_pickle

print(
    f"Initial memory usage: {round(int(process.memory_info().rss / 1024) / 1024, 3)}MiB"
)
pickle.Unpickler(io.BytesIO(b)).load()
print(
    f"After 1st unpickle memory usage: {round(int(process.memory_info().rss / 1024) / 1024, 3)}MiB"
)
for i in range(1000):
    pickle.Unpickler(io.BytesIO(b)).load()

print(
    f"Final memory usage: {round(int(process.memory_info().rss / 1024) / 1024, 3)}MiB"
)
CPython versions tested on:

3.11

Operating systems tested on:

Linux, macOS

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先在受影響的 CPython 版本上執行提供的 Python 重現程式,並比較重複呼叫 pickle.Unpickler(...).load() 後的記憶體。追蹤用於這些巢狀字典的 C Unpickler 路徑;當重複反序列化不再讓所回報的額外記憶體維持配置狀態,且已為此情況新增回歸測試時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
c, python
領域
backend, performance
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。