Question about st.cache_data TTL and Memory Cleanup
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Quiet
- Tech stack
- python
- Domain
- documentation
Research direction
Start by reviewing the Streamlit cache_data TTL and cache management documentation referenced in the issue, then trace the documented behavior for expired entries and memory cleanup. Confirm whether the documentation explains background expiration, retained Python memory, and per-entry reclamation; done means adding a clear, supported explanation or identifying the appropriate documentation gap.
Written by the indexing model from the issue text.
Description
Hi Streamlit Team,
I have a question about how st.cache_data works with ttl, specifically around memory cleanup.
For example:
@st.cache_data(ttl=3600)
def load_data():
...
Scenario
At 10:00 AM, User 1 calls the function.
The function caches a DataFrame of about 1 GB.
The cache TTL is 1 hour.
No one calls this function again until 4:00 PM.
What I observed
While monitoring my application’s memory usage, I noticed that even though the TTL had expired several hours earlier, the process memory did not decrease. For example, after the cached DataFrame increased the application’s memory to around 1 GB, the memory usage stayed close to 1 GB instead of dropping after the TTL expired.
This made me wonder:
Is this the expected behavior?
Does Streamlit have any background process that automatically removes expired cache entries from memory, or are expired entries only cleaned up when the cached function is called again?
If a cache entry has expired, is it removed from Streamlit’s cache while the underlying Python memory is still retained? If so, is there a recommended way to release or reclaim that memory?
I looked into the available cache management options, including:
max_entries
st.cache_data.clear()
function_name.clear()
However, none of these seem ideal for my use case.
Using max_entries across many cached functions would increase cache evictions, leading to more cache misses and recomputation, which impacts page performance.
Similarly, st.cache_data.clear() or function_name.clear() removes all cached entries (globally or for a function), which isn’t practical in a production application where other users may still be relying on those cached results.
Is there a recommended approach for releasing memory used by expired cache entries without clearing the entire cache or aggressively limiting the cache size?
I also tested the behavior with a second user.
At 4:00 PM, User 2 called the same cached function with different input parameters.
The function created a new cached result of approximately 800 MB.
I expected the old cache entry to be released and the application’s memory usage to settle around 800 MB. Instead, the process memory increased from about 1 GB to approximately 1.8 GB, which suggests that the earlier cached data was still occupying memory while the new cache entry was added.
Is this also the expected behavior? If so, when is the memory used by an expired cache entry actually released?
I’d appreciate any clarification on how TTL expiration and memory management are intended to work internally.
Thank you!
- Dominant language
- JavaScript
- Stars
- 178
- Forks
- 705
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 39
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from streamlit/docs
-
docs needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
docs needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
docs
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
bug needs triage
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
docs needs triage
Difficulty 3/5 1-2 days Newbie friendliness 48/100
Similar issues
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100