Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions gleap/src/main/java/io/gleap/GleapChatMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,13 @@ public LinearLayout getComponent(Activity activity) {
}

public void clearComponent() {
if (this.avatarBitmap != null) {
this.avatarBitmap.recycle();
this.avatarBitmap = null;
}

if (this.topImageBitmap != null) {
this.topImageBitmap.recycle();
this.topImageBitmap = null;
}
// These bitmaps come from GleapImageLoader, which still holds each one
// in its in-memory cache. The loader owns their lifecycle (it evicts
// under memory pressure and lets GC reclaim native memory); recycling
// a cache-shared entry here leaves a recycled bitmap in the cache that
// throws when onTrimMemory measures it. Just drop the reference.
this.avatarBitmap = null;
this.topImageBitmap = null;

this.layout = null;
}
Expand Down
21 changes: 15 additions & 6 deletions gleap/src/main/java/io/gleap/GleapImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public Thread newThread(Runnable runnable) {
private static final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(cacheSizeKb()) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
// A consumer that recycled a shared entry would otherwise throw
// IllegalStateException here during evictAll/trimToSize.
return bitmap.isRecycled() ? 0 : bitmap.getByteCount() / 1024;
}
};
private static boolean trimCallbacksRegistered = false;
Expand Down Expand Up @@ -242,10 +244,14 @@ private static synchronized void registerTrimCallbacks(Context context) {
applicationContext.registerComponentCallbacks(new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) {
if (level >= TRIM_MEMORY_BACKGROUND) {
cache.evictAll();
} else if (level >= TRIM_MEMORY_UI_HIDDEN) {
cache.trimToSize(cache.size() / 2);
// Runs on the main thread; must never crash the host app.
try {
if (level >= TRIM_MEMORY_BACKGROUND) {
cache.evictAll();
} else if (level >= TRIM_MEMORY_UI_HIDDEN) {
cache.trimToSize(cache.size() / 2);
}
} catch (Exception ignored) {
}
}

Expand All @@ -255,7 +261,10 @@ public void onConfigurationChanged(Configuration newConfig) {

@Override
public void onLowMemory() {
cache.evictAll();
try {
cache.evictAll();
} catch (Exception ignored) {
}
}
});
trimCallbacksRegistered = true;
Expand Down