Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsbeautifier uses too much memory and never releases that #2327

Open
papaya-ais opened this issue Nov 5, 2024 · 0 comments
Open

jsbeautifier uses too much memory and never releases that #2327

papaya-ais opened this issue Nov 5, 2024 · 0 comments

Comments

@papaya-ais
Copy link

papaya-ais commented Nov 5, 2024

Description

Hello.
I tried to beautify some javascript data in my python scripts by jsbeautifier.
But the result of it, my program started to use LARGE memory.

Input

The code looked like this before beautification:

for response_content in response_contents:
    import jsbeautifier
    self.response_contents.append(jsbeautifier.beautify(response_content))
    del jsbeautifier  # I tried to remove jsbeautifier from memory

    print("=================================== check memory")
    snapshot = tracemalloc.take_snapshot()
    for idx, stat in enumerate(snapshot.statistics('lineno')[:5], 1):
        print(str(stat))
        traces = tracemalloc.take_snapshot().statistics('traceback')
        for stat in traces[:1]:
            print("memory_blocks=", stat.count, "size_kB=", stat.size / 1024)
        for line in stat.traceback.format():
            print(line)
    print("=================================== check memory end")
    print(f"[__js_beautify] Beautified (len:{len(response_content)})")

Expected Output

The code should have looked like this after beautification:

=================================== check memory
=================================== check memory end
 [__js_beautify] Beautified (len:1613161)

I expected it would use less CPU and less memory.
image

Actual Output

The code actually looked like this after beautification:

=================================== check memory
/usr/lib/python3.8/json/decoder.py:353: size=111 MiB, count=14586, average=7976 B
/usr/local/lib/python3.8/dist-packages/jsbeautifier/core/token.py:28: size=58.0 MiB, count=524189, average=116 B
/usr/local/lib/python3.8/dist-packages/jsbeautifier/core/tokenizer.py:129: size=12.0 MiB, count=262092, average=48 B
<frozen importlib._bootstrap_external>:640: size=9192 KiB, count=56476, average=167 B
/usr/local/lib/python3.8/dist-packages/jsbeautifier/core/output.py:266: size=4005 KiB, count=7, average=572 KiB
('memory_blocks=', 14586, 'size_kB=', 113617.1328125)
   File "/usr/lib/python3.8/json/decoder.py", line 353
     obj, end = self.scan_once(s, idx)
=================================== check memory end
 [__js_beautify] Beautified (len:1613161)

But it started to use a huge amount of memory and a lot of CPU...
image

I think that it starts to use huge amounts of memory after full CPU usage

Steps to Reproduce

Environment

OS: Ubuntu 20.04 LTS
Python Version: 3.8.10

Settings

FROM ubuntu:20.04

ARG DEBIAN_FRONTEND=noninteractive

COPY ./source/requirements.txt /tmp/requirements.txt
RUN pip3 install -r /tmp/requirements.txt

WORKDIR /usr/share/MyProject
COPY ./source/ ./
COPY ./Entrypoint.sh /Entrypoint.sh
RUN chmod 777 /Entrypoint.sh
ENTRYPOINT ["/Entrypoint.sh"]
@papaya-ais papaya-ais changed the title jsbeautifier uses too much memory and never release that jsbeautifier uses too much memory and never releases that Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants