From 720519e4dec88dbd7f55d2239fe5706d6bac3d71 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Mon, 9 Nov 2026 15:42:05 +0100 Subject: [PATCH] fix: bound heap spikes behind OutOfMemoryError on low-RAM devices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0.19.19 still OOMed on a Samsung A30 (Android 11, 192MB heap). The notification-extras guard stopped the fatal read, but the binder-thread stack is only the victim allocation — several unbounded paths could fill the heap first: - Wallpaper and glyph-editor image imports decoded full-resolution bitmaps (a modern photo is tens to hundreds of MB) before scaling. - The in-app browser never destroyed its WebView, leaking a renderer and page state on every visit. - HttpRequest actions drained the entire response body into memory. - Every notification post/remove re-unparceled every active notification's extras; bursts stacked the parcel churn. Decodes now go through a sampled-decode helper (~1-2k px ceiling), the browser WebView is destroyed on release, the response drain caps at 256KB, and shade refreshes debounce 500ms so bursts collapse into one unparcel pass. largeHeap is enabled for headroom on 3-4GB devices. Authored By: TDvorak Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- CHANGELOG.md | 5 ++++ app/src/main/AndroidManifest.xml | 1 + .../AutomationNotificationListener.kt | 7 +++++ .../controllers/RealActionExecutor.kt | 15 ++++++++-- .../ui/components/WallpaperActionEditor.kt | 7 ++--- .../ui/screens/GlyphEditorScreen.kt | 6 ++-- .../ui/screens/IntegratedBrowserScreen.kt | 22 +++++++++++++- .../nothingmodes/ui/util/BitmapDecode.kt | 30 +++++++++++++++++++ 8 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/util/BitmapDecode.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 518d4fff..b1681113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### Fixed +- `OutOfMemoryError` on low-memory devices (0.19.19, Samsung A30 / Android 11, 192MB heap). Several unbounded allocations are now capped: the wallpaper and glyph-editor image imports decode sampled instead of full-resolution, the in-app browser destroys its WebView on exit instead of leaking it, `HttpRequest` actions drain at most 256KB of a response, and the shade refresh is debounced so notification bursts can't stack parcel-unpack churn. `largeHeap` is enabled for headroom on 3-4GB devices. + ## [0.19.19] ### Fixed diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a50b3d1c..45f9886b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -37,6 +37,7 @@ + val buf = ByteArray(8192) + var remaining = MAX_DRAIN_BYTES + while (remaining > 0) { + val n = stream.read(buf, 0, minOf(buf.size, remaining)) + if (n < 0) break + remaining -= n + } + } if (code in 200..299) { ActionResult.Success } else { @@ -2173,6 +2183,7 @@ class RealActionExecutor( private const val NOTIFICATION_ID_BASE = 2000 private const val VERIFY_ATTEMPTS = 4 private const val VERIFY_SETTLE_MS = 700L + private const val MAX_DRAIN_BYTES = 256 * 1024 /** Factory: creates a RealActionExecutor with all Android controllers. */ fun create( diff --git a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/components/WallpaperActionEditor.kt b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/components/WallpaperActionEditor.kt index 82d62866..0f68442b 100644 --- a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/components/WallpaperActionEditor.kt +++ b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/components/WallpaperActionEditor.kt @@ -3,7 +3,6 @@ package com.tdvorak.nothingmodes.ui.components import android.content.Context import android.content.Intent import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.graphics.ImageDecoder import android.net.Uri import android.os.Build @@ -32,6 +31,7 @@ import com.tdvorak.nothingmodes.ui.theme.NothingEnumSelector import com.tdvorak.nothingmodes.ui.theme.NothingInput import com.tdvorak.nothingmodes.ui.theme.NothingPrimaryButton import com.tdvorak.nothingmodes.ui.theme.NothingSpacing +import com.tdvorak.nothingmodes.ui.util.decodeSampledBitmap /** * Editor for [Action.SetWallpaper] that lets the user pick an image from the @@ -117,10 +117,7 @@ private fun loadWallpaperThumbnail( val uri = runCatching { Uri.parse(uriString) }.getOrNull() ?: return null return runCatching { - context.contentResolver - .openInputStream(uri) - ?.use { BitmapFactory.decodeStream(it) } - ?.scaledToMax(512) + decodeSampledBitmap(context.contentResolver, uri, 1024)?.scaledToMax(512) }.getOrNull() ?: runCatching { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { diff --git a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/GlyphEditorScreen.kt b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/GlyphEditorScreen.kt index 7ac815d2..ce846cdf 100644 --- a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/GlyphEditorScreen.kt +++ b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/GlyphEditorScreen.kt @@ -4,7 +4,6 @@ import android.content.ClipboardManager import android.content.Context import android.content.Intent import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.graphics.ImageDecoder import android.graphics.Typeface import android.media.MediaMetadataRetriever @@ -93,6 +92,7 @@ import com.tdvorak.nothingmodes.ui.theme.NothingSpacing import com.tdvorak.nothingmodes.ui.theme.NothingTag import com.tdvorak.nothingmodes.ui.theme.NothingTopBar import com.tdvorak.nothingmodes.ui.theme.ThemeManager +import com.tdvorak.nothingmodes.ui.util.decodeSampledBitmap import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.launch @@ -496,7 +496,7 @@ fun GlyphEditorScreen( ImportType.GIF, -> { runCatching { - context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it) } + decodeSampledBitmap(context.contentResolver, uri, 1024) }.getOrNull() ?: runCatching { val source = ImageDecoder.createSource(context.contentResolver, uri) @@ -545,7 +545,7 @@ fun GlyphEditorScreen( if (uri != null) { val bitmap = runCatching { - context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it) } + decodeSampledBitmap(context.contentResolver, uri, 1024) }.getOrNull() if (bitmap != null) applyBitmap(bitmap) else Toast.makeText(context, "Could not load image", Toast.LENGTH_SHORT).show() } else { diff --git a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/IntegratedBrowserScreen.kt b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/IntegratedBrowserScreen.kt index 5735b014..fe06b563 100644 --- a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/IntegratedBrowserScreen.kt +++ b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/screens/IntegratedBrowserScreen.kt @@ -316,11 +316,16 @@ fun IntegratedBrowserScreen( View(ctx) } }, + // A WebView left alive keeps its renderer and page state — + // repeated browser visits otherwise pile up toward OOM. + onRelease = { (it as? WebView)?.destroy() }, ) } } } +private const val MAX_IMPORT_CHARS = 1_000_000 + private suspend fun fetchText(url: String): String? = withContext(Dispatchers.IO) { runCatching { @@ -328,7 +333,22 @@ private suspend fun fetchText(url: String): String? = conn.connectTimeout = 10_000 conn.readTimeout = 10_000 try { - conn.inputStream.bufferedReader().use { it.readText() } + conn.inputStream.bufferedReader().use { reader -> + val buffer = CharArray(8192) + val out = StringBuilder() + while (out.length < MAX_IMPORT_CHARS) { + val n = reader.read(buffer) + if (n < 0) break + out.append(buffer, 0, n) + } + // Cut off mid-document means the payload exceeded the cap — + // fail rather than hand truncated JSON to the importer. + if (out.length >= MAX_IMPORT_CHARS && reader.read() >= 0) { + null + } else { + out.toString() + } + } } finally { conn.disconnect() } diff --git a/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/util/BitmapDecode.kt b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/util/BitmapDecode.kt new file mode 100644 index 00000000..d0251bfe --- /dev/null +++ b/ui/src/main/kotlin/com/tdvorak/nothingmodes/ui/util/BitmapDecode.kt @@ -0,0 +1,30 @@ +package com.tdvorak.nothingmodes.ui.util + +import android.content.ContentResolver +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.net.Uri + +/** + * Decodes [uri] with `inSampleSize` so the result's long edge lands near + * [maxDim]. `BitmapFactory.decodeStream` alone materializes the full image — + * a modern photo or video frame can exceed the whole heap on low-RAM + * devices (OutOfMemoryError reports on a 192MB-heap Samsung A30). + */ +fun decodeSampledBitmap( + resolver: ContentResolver, + uri: Uri, + maxDim: Int, +): Bitmap? { + val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } + resolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) } + if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return null + var sample = 1 + while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= maxDim) { + sample *= 2 + } + val options = BitmapFactory.Options().apply { inSampleSize = sample } + return resolver.openInputStream(uri)?.use { + BitmapFactory.decodeStream(it, null, options) + } +}