adds a mutex to make sure memory clearing only happens once at a time.

This commit is contained in:
Vitor Pamplona
2025-03-31 11:09:23 -04:00
parent 396831456c
commit b762907d69
@@ -71,6 +71,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.util.concurrent.atomic.AtomicBoolean
@Stable @Stable
class ServiceManager( class ServiceManager(
@@ -84,6 +85,8 @@ class ServiceManager(
private var collectorJob: Job? = null private var collectorJob: Job? = null
var isTrimmingMemoryMutex = AtomicBoolean(false)
private fun start(account: Account) { private fun start(account: Account) {
this.account = account this.account = account
start() start()
@@ -259,6 +262,8 @@ class ServiceManager(
} }
suspend fun trimMemory() { suspend fun trimMemory() {
if (isTrimmingMemoryMutex.compareAndSet(false, true)) {
try {
LocalCache.cleanObservers() LocalCache.cleanObservers()
val accounts = val accounts =
@@ -274,6 +279,10 @@ class ServiceManager(
LocalCache.prunePastVersionsOfReplaceables() LocalCache.prunePastVersionsOfReplaceables()
LocalCache.pruneExpiredEvents() LocalCache.pruneExpiredEvents()
} }
} finally {
isTrimmingMemoryMutex.getAndSet(false)
}
}
} }
// This method keeps the pause/start in a Syncronized block to // This method keeps the pause/start in a Syncronized block to