Skip to content

Commit 62767b1

Browse files
committed
Merge pull request square#359 from square/dimitris/oom
Catch OOM.
2 parents 8402ffe + cacac19 commit 62767b1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

picasso/src/main/java/com/squareup/picasso/BitmapHunter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.net.NetworkInfo;
2323
import android.net.Uri;
2424
import java.io.IOException;
25+
import java.io.PrintWriter;
26+
import java.io.StringWriter;
2527
import java.util.ArrayList;
2628
import java.util.List;
2729
import java.util.concurrent.Future;
@@ -89,6 +91,11 @@ protected void setExifRotation(int exifRotation) {
8991
} catch (IOException e) {
9092
exception = e;
9193
dispatcher.dispatchRetry(this);
94+
} catch (OutOfMemoryError e) {
95+
StringWriter writer = new StringWriter();
96+
stats.createSnapshot().dump(new PrintWriter(writer));
97+
exception = new RuntimeException(writer.toString(), e);
98+
dispatcher.dispatchFailed(this);
9299
} catch (Exception e) {
93100
exception = e;
94101
dispatcher.dispatchFailed(this);

picasso/src/test/java/com/squareup/picasso/BitmapHunterTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ public class BitmapHunterTest {
9898
verify(dispatcher).dispatchFailed(hunter);
9999
}
100100

101+
@Test public void outOfMemoryDispatchFailed() throws Exception {
102+
when(stats.createSnapshot()).thenReturn(mock(StatsSnapshot.class));
103+
104+
Action action = mockAction(URI_KEY_1, URI_1);
105+
BitmapHunter hunter = new OOMBitmapHunter(picasso, dispatcher, cache, stats, action);
106+
try {
107+
hunter.run();
108+
} catch (Throwable t) {
109+
Exception exception = hunter.getException();
110+
verify(dispatcher).dispatchFailed(hunter);
111+
verify(stats).createSnapshot();
112+
assertThat(hunter.getResult()).isNull();
113+
assertThat(exception).isNotNull();
114+
assertThat(exception.getCause()).isInstanceOf(OutOfMemoryError.class);
115+
}
116+
}
117+
101118
@Test public void runWithIoExceptionDispatchRetry() throws Exception {
102119
Action action = mockAction(URI_KEY_1, URI_1);
103120
BitmapHunter hunter =
@@ -444,4 +461,16 @@ private static class TestableBitmapHunter extends BitmapHunter {
444461
return MEMORY;
445462
}
446463
}
464+
465+
private static class OOMBitmapHunter extends TestableBitmapHunter {
466+
467+
OOMBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
468+
Action action) {
469+
super(picasso, dispatcher, cache, stats, action);
470+
}
471+
472+
@Override Bitmap decode(Request data) throws IOException {
473+
throw new OutOfMemoryError();
474+
}
475+
}
447476
}

0 commit comments

Comments
 (0)