From d78000061e050ada44a5fdde07a1c89bdc0ef234 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 8 May 2022 13:22:10 +0100 Subject: [PATCH] reducing native memory footprint by releasing crypto instances after using them - they're being recreated via olmStore.read --- .../olm/src/main/kotlin/app/dapk/st/olm/OlmWrapper.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmWrapper.kt b/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmWrapper.kt index ceee2ba..6a5531a 100644 --- a/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmWrapper.kt +++ b/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmWrapper.kt @@ -254,7 +254,7 @@ class OlmWrapper( return readSession.firstNotNullOfOrNull { (_, session) -> kotlin.runCatching { - when (type.toInt()) { + when (type) { OlmMessage.MESSAGE_TYPE_PRE_KEY -> { if (session.matchesInboundSession(body.value)) { logger.matrixLog(CRYPTO, "matched inbound session, attempting decrypt") @@ -270,6 +270,8 @@ class OlmWrapper( session.decryptMessage(olmMessage)?.let { JsonString(it) }?.also { logger.crypto("alt flow identity: $senderKey : ${session.sessionIdentifier()}") olmStore.persistSession(senderKey, SessionId(session.sessionIdentifier()), session) + }.also { + session.releaseSession() } } } @@ -287,6 +289,8 @@ class OlmWrapper( }.ifNull { logger.matrixLog(CRYPTO, "failed to decrypt olm session") DecryptionResult.Failed(errors.joinToString { it.message ?: "N/A" }) + }.also { + readSession.forEach { it.second.releaseSession() } } } @@ -310,7 +314,9 @@ class OlmWrapper( errorTracker.track(it) DecryptionResult.Failed(it.message ?: "Unknown") } - ) + ).also { + megolmSession.releaseSession() + } } } }