Skip to content

The object returned by json.loads(s) cannot release memory after calling the del method #139839

@xinsheng3

Description

@xinsheng3

Bug report

Bug description:

import base64
import json
import random
import string
import sys
import uuid
import time
import zlib

import psutil


def generate_large_file(filename='large_file', num_objects=1):
    json_obj = dict()
    for i in range(num_objects):
        name = ''.join(random.choice(string.ascii_lowercase) for _ in range(10))
        age = random.randint(18, 100)
        uuid_key = uuid.uuid4()
        uu1 = uuid.uuid4()
        uu2 = uuid.uuid4()
        uu3 = uuid.uuid4()
        uu4 = uuid.uuid4()
        uu5 = uuid.uuid4()
        uu6 = uuid.uuid4()

        json_obj[str(uuid_key)] = {"name": name,
                                   "age": age,
                                   str(uu1): {"name": name, "age": age,
                                              str(uu2): {"name": name, "age": age,
                                                         str(uu3): {"name": name, "age": age,
                                                                    str(uu4): {"name": name, "age": age,
                                                                               str(uu5): {"name": name, "age": age,
                                                                                          str(uu6): {"name": name, "age": age}}}}}},

                                   }

    json_obj = json.dumps(json_obj)
    zlib_obj = zlib.compress(json_obj.encode('utf-8'))
    b64_obj = base64.b64encode(zlib_obj)
    with open(filename, "wb") as f:
        f.write(b64_obj)


def get_process_memory(num):
    mem_info = psutil.Process().memory_info()
    res = mem_info.rss / (1024 ** 2)
    print("xxx %s process memory usage is %s M" % (num, res))


generate_large_file(num_objects=int(sys.argv[1]))

get_process_memory(1)

with open("./large_file", "rb") as f:
    content = f.read()
b64 = base64.b64decode(content)
z_content = zlib.decompress(b64)
data = json.loads(z_content)
get_process_memory(2)

del content
del b64
del z_content
del data

time.sleep(3)
get_process_memory(3)

test_github_issue.py test:

[root@localhost opt]# python3 test_github_issue.py 61000
xxx 1 process memory usage is 40.01171875 M
xxx 2 process memory usage is 281.01171875 M
xxx 3 process memory usage is 141.24609375 M

[root@localhost opt]# python3 test_github_issue.py 111000
xxx 1 process memory usage is 29.1640625 M
xxx 2 process memory usage is 430.328125 M
xxx 3 process memory usage is 25.44140625 M

why "xxx 3" - "xxx 1" != 0 ?

Python3.9.0 or later. The above verification data is in Python 3.11.6.

CPython versions tested on:

3.9

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions