C Unpickler memory leak when unpickling big geojsons
未关闭
还没有人认领这个 Issue。
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先在受影响的 CPython 版本上运行提供的 Python 复现程序,并比较重复调用 pickle.Unpickler(...).load() 后的内存。跟踪用于这些嵌套字典的 C Unpickler 路径;当重复反序列化不再使报告的额外内存保持分配状态,并且已为该情况添加回归测试时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- backend, performance
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100