Skip to content

Potential memory leak from TextIOWrapper which would hold unnecessary number of arenas #104954

Open
@itachaaa

Description

@itachaaa

sample code:

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import json
import time


def loads_json_from_file(content):
    if not content:
        return None
    return json.loads(content)


def generate():
    members = []
    for i in range(200):
        with open("./2.json", 'r') as fb:
            member = loads_json_from_file(fb.read())
        members.append(member)
    del members
    import gc; gc.collect()


if __name__ == '__main__':
    generate()
    while 1:
        time.sleep(10)

Use python -m memray run json_rep.py to generate flamefile:
image

Bug report

Theoretically, the del variables should not occupy any memory. However, the printed flame chart shows that the memory applied for by json.loads is not released, which seems to cause memory leakage.

Your environment

env:
└─$ uname -a
Linux DESKTOP-IEKI9L0 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
└─$ python --version
Python 3.10.6

  • CPython versions tested on: Python 3.10.6
  • Operating system and architecture: Linux DESKTOP-IEKI9L0 5.15.90.1-microsoft-standard-WSL2 Support "bpo-" in Misc/NEWS #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions