Skip to content

Commit 08e6c72

Browse files
committed
Improve low memory breadcrumb capturing
1 parent 286b57f commit 08e6c72

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,36 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) {
9191

9292
@Override
9393
public void onLowMemory() {
94-
final long now = System.currentTimeMillis();
95-
executeInBackground(() -> captureLowMemoryBreadcrumb(now, null));
94+
// we do this in onTrimMemory below already, this is legacy API (14 or below)
9695
}
9796

9897
@Override
9998
public void onTrimMemory(final int level) {
99+
if (level < TRIM_MEMORY_BACKGROUND) {
100+
// only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or
101+
// TRIM_MEMORY_COMPLETE.
102+
// Release as much memory as the process can.
103+
104+
// TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_RUNNING_LOW and
105+
// TRIM_MEMORY_RUNNING_CRITICAL.
106+
// Release any memory that your app doesn't need to run.
107+
// So they are still not so critical at the point of killing the process.
108+
// https://developer.android.com/topic/performance/memory
109+
return;
110+
}
111+
100112
final long now = System.currentTimeMillis();
101113
executeInBackground(() -> captureLowMemoryBreadcrumb(now, level));
102114
}
103115

104-
private void captureLowMemoryBreadcrumb(final long timeMs, final @Nullable Integer level) {
116+
private void captureLowMemoryBreadcrumb(final long timeMs, final int level) {
105117
if (scopes != null) {
106118
final Breadcrumb breadcrumb = new Breadcrumb(timeMs);
107-
if (level != null) {
108-
// only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or
109-
// TRIM_MEMORY_COMPLETE.
110-
// Release as much memory as the process can.
111-
112-
// TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_RUNNING_LOW and
113-
// TRIM_MEMORY_RUNNING_CRITICAL.
114-
// Release any memory that your app doesn't need to run.
115-
// So they are still not so critical at the point of killing the process.
116-
// https://developer.android.com/topic/performance/memory
117-
118-
if (level < TRIM_MEMORY_BACKGROUND) {
119-
return;
120-
}
121-
breadcrumb.setData("level", level);
122-
}
123-
124119
breadcrumb.setType("system");
125120
breadcrumb.setCategory("device.event");
126121
breadcrumb.setMessage("Low memory");
127122
breadcrumb.setData("action", "LOW_MEMORY");
123+
breadcrumb.setData("level", level);
128124
breadcrumb.setLevel(SentryLevel.WARNING);
129125
scopes.addBreadcrumb(breadcrumb);
130126
}

sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,6 @@ class AppComponentsBreadcrumbsIntegrationTest {
9595
sut.close()
9696
}
9797

98-
@Test
99-
fun `When low memory event, a breadcrumb with type, category and level should be set`() {
100-
val sut = fixture.getSut()
101-
val options = SentryAndroidOptions().apply {
102-
executorService = ImmediateExecutorService()
103-
}
104-
val scopes = mock<IScopes>()
105-
sut.register(scopes, options)
106-
sut.onLowMemory()
107-
verify(scopes).addBreadcrumb(
108-
check<Breadcrumb> {
109-
assertEquals("device.event", it.category)
110-
assertEquals("system", it.type)
111-
assertEquals(SentryLevel.WARNING, it.level)
112-
}
113-
)
114-
}
115-
11698
@Test
11799
fun `When trim memory event with level, a breadcrumb with type, category and level should be set`() {
118100
val sut = fixture.getSut()

0 commit comments

Comments
 (0)