|
12 | 12 | import io.sentry.Integration; |
13 | 13 | import io.sentry.SentryLevel; |
14 | 14 | import io.sentry.SentryOptions; |
| 15 | +import io.sentry.android.core.internal.util.AndroidCurrentDateProvider; |
| 16 | +import io.sentry.android.core.internal.util.Debouncer; |
15 | 17 | import io.sentry.android.core.internal.util.DeviceOrientations; |
16 | 18 | import io.sentry.protocol.Device; |
17 | 19 | import io.sentry.util.Objects; |
|
24 | 26 | public final class AppComponentsBreadcrumbsIntegration |
25 | 27 | implements Integration, Closeable, ComponentCallbacks2 { |
26 | 28 |
|
| 29 | + private static final long DEBOUNCE_WAIT_TIME_MS = 60 * 1000; |
| 30 | + // pre-allocate hint to avoid creating it every time for the low memory case |
| 31 | + private static final @NotNull Hint EMPTY_HINT = new Hint(); |
| 32 | + |
27 | 33 | private final @NotNull Context context; |
28 | 34 | private @Nullable IScopes scopes; |
29 | 35 | private @Nullable SentryAndroidOptions options; |
30 | 36 |
|
| 37 | + private final @NotNull Debouncer trimMemoryDebouncer = |
| 38 | + new Debouncer(AndroidCurrentDateProvider.getInstance(), DEBOUNCE_WAIT_TIME_MS, 0); |
| 39 | + |
31 | 40 | public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { |
32 | 41 | this.context = |
33 | 42 | Objects.requireNonNull(ContextUtils.getApplicationContext(context), "Context is required"); |
@@ -91,42 +100,43 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) { |
91 | 100 |
|
92 | 101 | @Override |
93 | 102 | public void onLowMemory() { |
94 | | - final long now = System.currentTimeMillis(); |
95 | | - executeInBackground(() -> captureLowMemoryBreadcrumb(now, null)); |
| 103 | + // we do this in onTrimMemory below already, this is legacy API (14 or below) |
96 | 104 | } |
97 | 105 |
|
98 | 106 | @Override |
99 | 107 | public void onTrimMemory(final int level) { |
| 108 | + if (level < TRIM_MEMORY_BACKGROUND) { |
| 109 | + // only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or |
| 110 | + // TRIM_MEMORY_COMPLETE. |
| 111 | + // Release as much memory as the process can. |
| 112 | + |
| 113 | + // TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_RUNNING_LOW and |
| 114 | + // TRIM_MEMORY_RUNNING_CRITICAL. |
| 115 | + // Release any memory that your app doesn't need to run. |
| 116 | + // So they are still not so critical at the point of killing the process. |
| 117 | + // https://developer.android.com/topic/performance/memory |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + if (trimMemoryDebouncer.checkForDebounce()) { |
| 122 | + // if we received trim_memory within 1 minute time, ignore this call |
| 123 | + return; |
| 124 | + } |
| 125 | + |
100 | 126 | final long now = System.currentTimeMillis(); |
101 | 127 | executeInBackground(() -> captureLowMemoryBreadcrumb(now, level)); |
102 | 128 | } |
103 | 129 |
|
104 | | - private void captureLowMemoryBreadcrumb(final long timeMs, final @Nullable Integer level) { |
| 130 | + private void captureLowMemoryBreadcrumb(final long timeMs, final int level) { |
105 | 131 | if (scopes != null) { |
106 | 132 | 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 | | - |
124 | 133 | breadcrumb.setType("system"); |
125 | 134 | breadcrumb.setCategory("device.event"); |
126 | 135 | breadcrumb.setMessage("Low memory"); |
127 | 136 | breadcrumb.setData("action", "LOW_MEMORY"); |
| 137 | + breadcrumb.setData("level", level); |
128 | 138 | breadcrumb.setLevel(SentryLevel.WARNING); |
129 | | - scopes.addBreadcrumb(breadcrumb); |
| 139 | + scopes.addBreadcrumb(breadcrumb, EMPTY_HINT); |
130 | 140 | } |
131 | 141 | } |
132 | 142 |
|
|
0 commit comments