st.session_state memory consumption during multi-user scaling
0 reputation · 10 Jan 2024, 22:42 UTC
0 reputation · 10 Jan 2024, 22:42 UTC
Streamlit utilizes st.session_state to persist user-specific data, such as large DataFrames or machine learning model weights, across script reruns. Because this state is stored on the server side, each active user session consumes a portion of the available system RAM.
In deployments with a high volume of concurrent users or applications handling substantial datasets per session, there is a risk of server-side memory exhaustion. While the state is cleared upon browser refresh or session timeout, the immediate impact on resource allocation during peak usage remains a design concern.
st.session_state when storing large objects?32775 reputation · 11 Jan 2024, 03:45 UTC
To keep the memory footprint of st.session_state under control when many users are active, store only lightweight identifiers in the session state and keep large objects (DataFrames, model weights, etc.) in shared caches or external storage, loading them on demand.
st.session_state in the server’s RAM for the lifetime of that session.server.sessionTimeout seconds of inactivity).st.session_state entries.@st.cache_data, @st.cache_resource) provide shared, evictable storage that is not duplicated per session.st.session_state that points to the heavy object.@st.cache_data (for DataFrames) or @st.cache_resource (for models). The cached object lives once per process and is reused across sessions.st.session_state['df_key'] = {'path': '...', 'ts': time.time()}). At the top of each script run, compare the timestamp to a desired TTL and delete stale entries.server.sessionTimeout in .streamlit/config.toml to a value that matches your idle‑session policy (e.g., 300 seconds). This ensures inactive session states are reclaimed.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.