Here is my code:
I am using the latest version of st and st_aggrid, and all libs i used are updated.
from memory_profiler import profile
import pandas as pd
import streamlit as st
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
def interactive_datatable1(df):
return_mode_value = DataReturnMode.__members__["AS_INPUT"]
update_mode_value = GridUpdateMode.__members__['SELECTION_CHANGED']
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(groupable=True,headerCheckboxSelection=True,value=True,
enableRowGroup=True, aggFunc='sum', editable=True)
gb.configure_selection('multiple',
use_checkbox=True, groupSelectsChildren=True,
groupSelectsFiltered=True)
gb.configure_grid_options(domLayout='normal')
gridOptions = gb.build()
grid_response = AgGrid(
df,
gridOptions=gridOptions,
data_return_mode=return_mode_value,
update_mode=update_mode_value,
fit_columns_on_grid_load=False,
theme='material'
)
return grid_response
@profile
def main():
df=pd.read_pickle('df.pkl')
a=interactive_datatable1(df)
main()
Here the size of df.pkl is about 3.2 MB.
Next is the memory profile:
Line # Mem usage Increment Occurrences Line Contents
=============================================================
26 375.5 MiB 375.5 MiB 1 @profile
27 def main():
28 376.2 MiB 0.8 MiB 1 df=pd.read_pickle('df.pkl')
29 379.2 MiB 3.0 MiB 1 a=interactive_datatable1(df)
As you can see, every time the code runs, the memory usage increases. This is because the table created by st_aggrid is not destroyed. If I remove st_aggrid, the memory usage does not increase, and the df.pkl file can be released. Now, I want to know how to solve this memory leak issue. I tried adding a key to each specific content dataframe and using the hash value as the key. This reduces the memory usage of st_aggrid, but it does not solve the problem. I also want to know if it is possible to directly access the widget objects created by st and delete the unused ones?