Python tkinter memory leak problem.

Asked 2 years ago, Updated 2 years ago, 75 views

import tracemalloc
tracemalloc.start(10)

snap1 = [] #Check initial value
def test():
    if not snap1:
        snap1 = tracemalloc.take_snapshot()
    else:
        lines = []
        top_stats = tracemalloc.take_snapshot().compare_to(snap1, 'lineno')
            # Obtain and output 10 in the order of memory usage
        for stat in top_stats[:10]:
           lines.append(str(stat))
        print('\n'.join(lines), flush=True)

Hello, I'm a beginner developer. The code under development is python 3.9, and the gui is Tkinter, which is powered by cpu 8-10% and RAM 80-90mbyte at initial execution, so memory increases 10 times and cpu increases 2~3 times at about a day's operation.

The test function is executed every hour using the scheduler.

What I'm curious about is that I'm trying to track the memory accumulated and increased during the program execution time with tracemalloc, but it's difficult because the current code shows only the first value and function called.

And let me ask you a question.

def test2k():
    global img_st_22
    try:
        # a red light
        img_st_2 = Image.open(img_list_path[6]) #global must be added to prevent erasing. Garbageburg
        img_st_2 = img_st_2.resize((14, 14), Image.ANTIALIAS)

        img_st_11 = ImageTk.PhotoImage(img_st_1)

        r_canvas4.create_image(35, 105, image=img_st_22, anchor="w")
       #test1111 =  r_canvas4.create_image(35, 105, image=img_st_22, anchor="w")
    ....

I'm using it by adding global in this way, but if I repeatedly call the test2k function, I wonder if the object is created indefinitely, or if it's good to put it into a variable with test1111 and delete it with del test11111.

The first priority for tracemalloc operation is PIL, and there are about 150 images included in the gui. I don't know if it's because it's an initial run or if you're looking for it...

As I taught myself, I think I lack a lot of basic skills. I need your help

Add Edit

..\venv\lib\site-packages\PIL\PngImagePlugin.py:482: size=45.6 KiB (+43.9 KiB), count=472 (+432), average=99 B
..\Python39\lib\ctypes\__init__.py:102: size=61.6 KiB (+24.9 KiB), count=398 (+157), average=158 B
..\Python39\lib\tkinter\__init__.py:1628: size=18.3 KiB (+17.2 KiB), count=293 (+276), average=64 B
..\Python39\lib\tkinter\__init__.py:813: size=12.7 KiB (+12.7 KiB), count=203 (+203), average=64 B
..\Python39\lib\tkinter\__init__.py:1483: size=9510 B (+9054 B), count=93 (+88), average=102 B
../test.py:182: size=7410 B (+7353 B), count=154 (+153), average=48 B
..\Python39\lib\tkinter\__init__.py:111: size=6552 B (+5688 B), count=129 (+111), average=51 B
..\test.py:718: size=5512 B (+5512 B), count=42 (+42), average=131 B
..\Python39\lib\tkinter\__init__.py:805: size=5440 B (+5440 B), count=12 (+12), average=453 B
..\Python39\lib\tracemalloc.py:558: size=4808 B (+4752 B), count=75 (+74), average=64 B

python memory tkinter

2022-09-20 11:12

1 Answers

As far as I know, Python manages its own memory.

In this case, there is an object that cannot be managed on its own, and in the case of an object in a function, it will fly away when the function ends, and it is very ambiguous to advise other masters because there is no full code.

What I can advise from the same beginner's point of view is to remove the list outside the function, and remove the variables that you use as global.

In my case, I have experienced a memory problem because I added elements repeatedly without deleting the list.

If you want advice from other masters, I recommend that you also upload error messages and command execution codes.


2022-09-20 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.