I have a cache object designed to hold variables shared across modules.
Concurrent threads are accessing & writing to that cache object using a cache handler that uses variable locking to keep the cache integrity.
The cache holds all variables under a unique session id. When a new session id is created, it has to reference the old one, copying some of the variables into a new session.
Some session ids will be running concurrently too.
I need to clear out the cache as soon as all concurrent threads are done referencing it, and all new sessions have copied the variables from it into their session.
The problem...
I don't know when it's safe to clear the cache.
I have hundreds of threads making API calls of variable time.  New sessions will spawn new threads.  I can't just look at active threads to determine when to clear the cache.  
My cache will grow to infinite size & eventually crash the program.
I believe this must be a common problem.  And those far smarter then me have through I through.
Any ideas how to best solve this?
You can solve this issue with locking... create a Cleaner thread that sometimes locks the cache (take all the locks that you have on it) and clears it, then release the locks...
(if you have many locks for different part of the cache, you can also lock & clear the cache piece by piece...)
Cache growing infinitely large because of concurrent threads