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

memory leak problem #4994

Open
1 task
viethoang261 opened this issue Feb 28, 2025 · 0 comments
Open
1 task

memory leak problem #4994

viethoang261 opened this issue Feb 28, 2025 · 0 comments

Comments

@viethoang261
Copy link

viethoang261 commented Feb 28, 2025

Duplicate Check

Describe the bug

When using custom control, if you create an attribute to manage data or do some state management, those will not be freed up when that control is removed from the parent, so it will cause a memory leak, even using gc.collect() won't help

Code sample

Code
import flet as ft

class FirstPage(ft.Column):
    def __init__(self):
        super().__init__()
        self.alignment = ft.MainAxisAlignment.CENTER
        self.horizontal_alignment = ft.CrossAxisAlignment.CENTER
        
        size_in_mb = 100
        string_sample = "Hello, world!"  # Example string (~13 bytes)
        num_strings = (size_in_mb * 1024 * 1024) // len(string_sample)  # Calculate how many fit in 100MB
        self.string_list = [string_sample] * num_strings  # Create the list
        self.strings = ', '.join(self.string_list)
        self.strings = ', '.join(self.string_list)
        self.controls = [
            ft.Text("First Page"),
            ft.Text(f"total length: {len(self.string_list)}"),
            ft.ElevatedButton("Go to Second Page", on_click=self.go_to_second_page),
        ]
        
    def will_unmount(self):
        super().will_unmount()
        
    def go_to_second_page(self, e):
        self.page.go("/second")
        
class SecondPage(ft.Column):
    def __init__(self):
        super().__init__()
        self.alignment = ft.MainAxisAlignment.CENTER
        self.horizontal_alignment = ft.CrossAxisAlignment.CENTER
        
        size_in_mb = 100
        string_sample = "Hello, world!"  # Example string (~13 bytes)
        num_strings = (size_in_mb * 1024 * 1024) // len(string_sample)  # Calculate how many fit in 100MB
        self.string_list = [string_sample] * num_strings  # Create the list
        self.strings = ', '.join(self.string_list)
        self.controls = [
            ft.Text("Second Page"),
            ft.Text(f"total length: {len(self.string_list)}"),
            ft.ElevatedButton("Go to First Page", on_click=self.go_to_first_page),
        ]
        
    def will_unmount(self):
        super().will_unmount()
        
    def go_to_first_page(self, e):
        self.page.go("/first")
        

def main(page: ft.Page):
    page.title = "Flet memory example"
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.window.width = 800
    page.window.height = 600

    def route_change(_):
        if page.route == "/first":
            page.controls.clear()
            page.add(FirstPage())
        elif page.route == "/second":
            page.controls.clear()
            page.add(SecondPage())
        page.update()

    page.on_route_change = route_change
    
    page.go("/first")
    

ft.app(main)

To reproduce

...

Expected behavior

No response

Screenshots / Videos

Captures first when starting the app

Image

after switching pages several times

Image

Operating System

Linux

Operating system details

ubuntu22

Flet version

latest

Regression

No, it isn't

Suggestions

I found a solution: clear all class attributes in the will_unmount event, and the memory leak is gone

Image

after applying the code
when starting the app

Image

after switching pages several times

Image

Hope this small modification will help

Logs

Logs
[Paste your logs here]

Additional details

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant