Skip to content

Commit

Permalink
Properly synchronize RequestManagers in Glide in onTrimMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Mar 28, 2020
1 parent cbbb254 commit 921ea46
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@
public class Glide implements ComponentCallbacks2 {
private static final String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
private static final String TAG = "Glide";

@GuardedBy("Glide.class")
private static volatile Glide glide;

private static volatile boolean isInitializing;

private final Engine engine;
Expand All @@ -122,7 +125,10 @@ public class Glide implements ComponentCallbacks2 {
private final ArrayPool arrayPool;
private final RequestManagerRetriever requestManagerRetriever;
private final ConnectivityMonitorFactory connectivityMonitorFactory;

@GuardedBy("managers")
private final List<RequestManager> managers = new ArrayList<>();

private final RequestOptionsFactory defaultRequestOptionsFactory;
private MemoryCategory memoryCategory = MemoryCategory.NORMAL;

Expand Down Expand Up @@ -173,6 +179,8 @@ public static File getPhotoCacheDir(@NonNull Context context, @NonNull String ca
* @return the singleton
*/
@NonNull
// Double checked locking is safe here.
@SuppressWarnings("GuardedBy")
public static Glide get(@NonNull Context context) {
if (glide == null) {
GeneratedAppGlideModule annotationGeneratedModule =
Expand Down Expand Up @@ -228,12 +236,14 @@ public static void init(@NonNull Context context, @NonNull GlideBuilder builder)
}

@VisibleForTesting
public static synchronized void tearDown() {
if (glide != null) {
glide.getContext().getApplicationContext().unregisterComponentCallbacks(glide);
glide.engine.shutdown();
public static void tearDown() {
synchronized (Glide.class) {
if (glide != null) {
glide.getContext().getApplicationContext().unregisterComponentCallbacks(glide);
glide.engine.shutdown();
}
glide = null;
}
glide = null;
}

@GuardedBy("Glide.class")
Expand Down Expand Up @@ -685,8 +695,10 @@ public void trimMemory(int level) {
Util.assertMainThread();
// Request managers need to be trimmed before the caches and pools, in order for the latter to
// have the most benefit.
for (RequestManager manager : managers) {
manager.onTrimMemory(level);
synchronized (managers) {
for (RequestManager manager : managers) {
manager.onTrimMemory(level);
}
}
// memory cache needs to be trimmed before bitmap pool to trim re-pooled Bitmaps too. See #687.
memoryCache.trimMemory(level);
Expand Down

0 comments on commit 921ea46

Please sign in to comment.