You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched the opened issues and there are no duplicates
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
importfletasftclassFirstPage(ft.Column):
def__init__(self):
super().__init__()
self.alignment=ft.MainAxisAlignment.CENTERself.horizontal_alignment=ft.CrossAxisAlignment.CENTERsize_in_mb=100string_sample="Hello, world!"# Example string (~13 bytes)num_strings= (size_in_mb*1024*1024) //len(string_sample) # Calculate how many fit in 100MBself.string_list= [string_sample] *num_strings# Create the listself.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),
]
defwill_unmount(self):
super().will_unmount()
defgo_to_second_page(self, e):
self.page.go("/second")
classSecondPage(ft.Column):
def__init__(self):
super().__init__()
self.alignment=ft.MainAxisAlignment.CENTERself.horizontal_alignment=ft.CrossAxisAlignment.CENTERsize_in_mb=100string_sample="Hello, world!"# Example string (~13 bytes)num_strings= (size_in_mb*1024*1024) //len(string_sample) # Calculate how many fit in 100MBself.string_list= [string_sample] *num_strings# Create the listself.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),
]
defwill_unmount(self):
super().will_unmount()
defgo_to_first_page(self, e):
self.page.go("/first")
defmain(page: ft.Page):
page.title="Flet memory example"page.vertical_alignment=ft.MainAxisAlignment.CENTERpage.horizontal_alignment=ft.CrossAxisAlignment.CENTERpage.window.width=800page.window.height=600defroute_change(_):
ifpage.route=="/first":
page.controls.clear()
page.add(FirstPage())
elifpage.route=="/second":
page.controls.clear()
page.add(SecondPage())
page.update()
page.on_route_change=route_changepage.go("/first")
ft.app(main)
To reproduce
...
Expected behavior
No response
Screenshots / Videos
Captures
first when starting the app
after switching pages several times
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
after applying the code
when starting the app
after switching pages several times
Hope this small modification will help
Logs
Logs
[Paste your logs here]
Additional details
No response
The text was updated successfully, but these errors were encountered:
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
To reproduce
...
Expected behavior
No response
Screenshots / Videos
Captures
first when starting the appafter switching pages several times
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
after applying the code
when starting the app
after switching pages several times
Hope this small modification will help
Logs
Logs
[Paste your logs here]
Additional details
No response
The text was updated successfully, but these errors were encountered: