I am trying to send a lot of images to an API
using okHttp3
and retrofit2
. I have tried sending the images from one device (5 images at a time) and it worked well. Then I tried sending 5 images from 5 devices at the same time and it worked well, but when I tried to send 30 images from 5 devices, I got the following error:
2020-01-17 14:57:07.416 32244-32264/my.example.com.puri W/zygote: Throwing OutOfMemoryError "Failed to allocate a 102554120 byte allocation with 8388608 free bytes and 97MB until OOM, max allowed footprint 174912000, growth limit 268435456"
2020-01-17 14:57:07.422 32244-32264/my.example.com.puri E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: my.example.com.puri, PID: 32244
java.lang.OutOfMemoryError: Failed to allocate a 102554120 byte allocation with 8388608 free bytes and 97MB until OOM, max allowed footprint 174912000, growth limit 268435456
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:178)
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:209)
at okio.Buffer.readString(Buffer.java:620)
at okio.Buffer.readString(Buffer.java:603)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:198)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
This error
does not come on the first device that sends request, rather it comes to the one that is 3rd or last. The device where I click the send
button first always succeeds.
I also made these changes to the Manifest
.
<application
android:hardwareAccelerated="false"
android:largeHeap="true"
>
And here is how I make the request.
@Multipart
@POST("installationdocument")
Call<Void> createNewDocument(@Header(AUTH_HEADER) String authorization, @Part ArrayList<MultipartBody.Part> images, @Part("document") RequestBody json);
And:
@Override
public void callRestApi(PuriRestApi restApi, RestApiJobService.ApiRequestor apiRequestor) {
MediaType JSON = MediaType.parse("application/json");
MediaType JPG = MediaType.parse("image/jpg");
ArrayList<MultipartBody.Part> imageParts = new ArrayList<>();
for (String imagePath : imagePaths) {
File image = new File(imagePath);
RequestBody imageBody = RequestBody.create(JPG, image);
imageParts.add(MultipartBody.Part.createFormData("images", image.getName(), imageBody));
}
RequestBody jsonPart = RequestBody.create(JSON, documentString);
apiRequestor.request(restApi.createNewDocument(authentication, imageParts, jsonPart), response -> {
if (response.isSuccessful()) {
if (imagePaths != null && imagePaths.length > 0) {
for (String imagePath : imagePaths) {
File image = new File(imagePath);
image.delete();
}
}
return true;
} else {
return false;
}
});
}
Maybe creating a large variable is what throws this error. Because variable ArrayList<MultipartBody.Part> imageParts
will contain around 150mb of image data with 30 images. But other than that I have no idea what could be causing this error.
Any kind of help is really appreciated.