How to stop RAM memory usage increasing each rerun when (re)creating a lightweight_charts StreamlitChart?

When creating a StreamlitChart object and running st.rerun(), the amount of RAM memory being used increases upon each rerun, even if the StreamlitChart object is empty, but it’s even worse when data is added to the chart.

My expectation would be that the StreamlitChart object would be cleared from RAM after a rerun.

I have tried to:

  • delete the objects and gc.collect();
  • assign the chart to a object in st.session_state, in order to reuse the graph;
  • use caching (st.cache_resource for the graph, st.cache_data for data set in the graph).

Below is the MVP + GitHub link incl. README to reproduce.

import os
import psutil
import pandas as pd
import streamlit as st
from lightweight_charts.widgets import StreamlitChart

def get_memory_usage():
    process = psutil.Process(os.getpid())
    memory_in_mb = process.memory_info().rss / (1024 * 1024)
    return memory_in_mb

# Creation of chart increases memory on each rerun. 
chart = StreamlitChart(toolbox=True, height=150, width=150, inner_width=1, inner_height=1)

if st.button("Rerun"):
    st.rerun()

if 'previous_ram' not in st.session_state:
    st.session_state.previous_ram = 0

ram = get_memory_usage()
ram_increase = ram - st.session_state.previous_ram

st.session_state.previous_ram = ram
st.write("Streamlit Cache and Memory Usage Demo")
st.write(f"Memory Usage: {ram:.2f} MB")
st.write(f"Memory Increase Since Last Run: {ram_increase:.2f} MB")

Minimum viable product to reproduce:

Live demo of problem:

Streamlit & Python version used:
streamlit==1.41.1
python==3.10.4

Thanks in advance for any help.

Although, I don’t have a useful answer for you, I’ve shared your post internally.

1 Like

Thank you, much appreciated.

This might be related to our forward message cache. Are you able to also reproduce the same issue if you configure global.storeCachedForwardMessagesInMemory to false?

1 Like

Hi Lukas,

If i understand correctly, this is what you mean, added this to config.toml (GH + Demo also updated):

[global] 
storeCachedForwardMessagesInMemory = false

Unfortunately the issue remains the same.

Edit: removed “global.” , lower case “false”