From 503b30600abd898d35e4ef18448b2ee62b4ff662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Wed, 20 Aug 2025 16:53:51 +0100 Subject: [PATCH] Add hprof dump on OOM for debug builds --- .../gadgetbridge/GBExceptionHandler.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBExceptionHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBExceptionHandler.java index c4f6f45e11..9a589474db 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBExceptionHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBExceptionHandler.java @@ -32,6 +32,8 @@ import androidx.core.app.NotificationCompat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; + import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils; @@ -66,10 +68,28 @@ public class GBExceptionHandler implements Thread.UncaughtExceptionHandler { } } + // Heap dump on OOM in debug builds + if (BuildConfig.DEBUG && (ex.getClass().equals(OutOfMemoryError.class) + || (ex.getCause() != null && ex.getCause().getClass().equals(OutOfMemoryError.class)))) { + try { + final File cacheDir = GBApplication.getContext().getExternalCacheDir(); + if (cacheDir != null) { + final File oomDir = new File(cacheDir.getAbsolutePath() + File.separator + "oom"); + //noinspection ResultOfMethodCallIgnored + oomDir.mkdirs(); + final String dumpPath = oomDir.getAbsolutePath() + File.separator + "oom-" + System.currentTimeMillis() + ".hprof"; + LOG.debug("Dumping hprof data to: {}", dumpPath); + android.os.Debug.dumpHprofData(dumpPath); + } + } catch (final Throwable t) { + LOG.error("Failed to dump hprof on oom", t); + } + } + if (mDelegate != null) { try { mDelegate.uncaughtException(thread, ex); - }catch (Throwable ignored){ + } catch (Throwable ignored) { } } else { System.exit(1);