streamlit-folium icon indicating copy to clipboard operation
streamlit-folium copied to clipboard

Potential memory leak with dynamic maps

Open adrianISI opened this issue 8 months ago • 2 comments
trafficstars

I have been greatly enjoying this repo, but I have recently experienced something I think might be a memory leak.

My goal is to dynamically load data and display it in the map without reloading the window. Since i have a lot of data I first visualise it as a FastMarkerCluster, then later draw the shapes once zoom level is large enough. My issue is related to the marker cluster that seems to not be deleted from cache in the browser. Each time new data is loaded, the memory increases. Below is a small sample code of the issue. Each time the button is clicked the memory usage increases. This will go on until the browser runs out of memory (increase N to have this happen faster)

import streamlit as st 
import numpy as np
from streamlit_folium import st_folium
import folium
from folium.plugins import FastMarkerCluster

def load_marker_data(N=10000):
    return np.random.rand(N, 2) - 0.5

if "marker_data" not in st.session_state:
    st.session_state["marker_data"] = load_marker_data()
m = folium.Map(location=[0, 0], zoom_start=8)
fg = folium.FeatureGroup()
fg.add_child(FastMarkerCluster(st.session_state["marker_data"]))
st_folium(m, feature_group_to_add=fg, use_container_width=True)

st.button("Simulate new data loading", on_click=lambda: st.session_state.update({"marker_data": load_marker_data()}))

I have tried many different variations to make this work (like clearing cache, rerunning and so on), but have so far been unsuccessful. The overall behaviour of the code above is good, the map is never redrawn, however the memory usage increasing is a problem.

It would be great if someone could help me with getting the correct behaviour or look into if this is an actual bug.

PS. duplicate of https://github.com/randyzwitch/streamlit-folium/issues/245 that i accidently closed.

adrianISI avatar Mar 02 '25 23:03 adrianISI

Is this old issue in markercluster related to this issue? https://github.com/Leaflet/Leaflet.markercluster/issues/977#issue-533244468

adrianISI avatar Mar 02 '25 23:03 adrianISI

Thanks for finding that! I was going to say, this feels like an upstream issue. However, @blackary is probably a better person to think through the logic of this, since we're already manipulating Folium in ways they might not expect (especially around when in the Streamlit eval cycle to re-draw maps).

Could you try and add the code snippet that the user in the link issue suggests, and let us know what you notice?

randyzwitch avatar Mar 03 '25 19:03 randyzwitch