ionic-team / ionic-team/capacitor-camera

[Bug]: OutOfMemory errors on Android on Camera.chooseFromGallery

Open
#56 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4
Forks
1
Avg merge
2d 1h
Merged PRs (30d)
2

Description

### Current Behavior

Since adopting Capacitor 8 and the new camera plugin (version `8.2.0`), we've seen quite a few occurrences of an out of memory issue - possibly related to `Camera.chooseFromGallery`.

It happens across different Android versions, and across many different devices with differing amounts of memory.

Based on the stack trace from the Sentry events we see, it's seemingly related to when users are selecting items from the gallery:
```
java.lang.OutOfMemoryError: Failed to allocate a 75497488 byte allocation with 25165824 free bytes and 36MB until OOM, target footprint 121071528, growth limit 134217728
at java.util.Arrays.copyOf(Arrays.java:3785)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:182)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:758)
at java.lang.StringBuilder.append(StringBuilder.java:261)
at org.json.JSONStringer.string(JSONStringer.java:354)
at org.json.JSONStringer.value(JSONStringer.java:261)
at org.json.JSONObject.writeTo(JSONObject.java:734)
at org.json.JSONStringer.value(JSONStringer.java:246)
at org.json.JSONArray.writeTo(JSONArray.java:616)
at org.json.JSONStringer.value(JSONStringer.java:242)
at org.json.JSONObject.writeTo(JSONObject.java:734)
at org.json.JSONStringer.value(JSONStringer.java:246)
at org.json.JSONObject.writeTo(JSONObject.java:734)
at org.json.JSONObject.toString(JSONObject.java:702)
at com.getcapacitor.PluginResult.toString(PluginResult.java:67)
at com.getcapacitor.MessageHandler.legacySendResponseMessage(MessageHandler.java:142)
at com.getcapacitor.MessageHandler.sendResponseMessage(MessageHandler.java:124)
at com.getcapacitor.PluginCall.resolve(PluginCall.java:55)
at com.capacitorjs.plugins.camera.IonCameraFlow.handleGalleryMediaResults(IonCameraFlow.kt:823)
at com.capacitorjs.plugins.camera.IonCameraFlow.access$handleGalleryMediaResults(IonCameraFlow.kt:46)
at com.capacitorjs.plugins.camera.IonCameraFlow$processResultFromGallery$1.invokeSuspend$lambda$0(IonCameraFlow.kt:907)
at com.capacitorjs.plugins.camera.IonCameraFlow$processResultFromGallery$1.$r8$lambda$r3wiH5jKGV7cKtd7j-_zGCrAVBU
at com.capacitorjs.plugins.camera.IonCameraFlow$processResultFromGallery$1$$ExternalSyntheticLambda0.invoke(D8$$SyntheticClass:0)
at io.ionic.libs.ioncameralib.manager.IONCAMRGalleryManager.onChooseFromGalleryResult(IONCAMRGalleryManager.kt:136)
at com.capacitorjs.plugins.camera.IonCameraFlow$processResultFromGallery$1.invokeSuspend(IonCameraFlow.kt:901)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:34)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:829)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)
```

The `onChooseFromGalleryResult` in the stack trace hints to me that it's related to the `chooseFromGallery`.
And granted, it's quite a large file, but I don't think it's unreasonably large.

Is there anything we can tweak on our side or is this a potential bug in 8.x.x of the Camera plugin?

### Expected Behavior

No OutOfMemory errors when selecting images from the gallery

### Reproduction

Haven't reproduced – only reported in our error reporting tool (Sentry)

### Screenshots / Media

_No response_

### Environment Details

```Shell
OS Version: Various versions of Android (seen it on 11, 12, 13, 14, 15 and 16)
```

### Versions Affected

8.2.0

### Platforms Affected

- [ ] iOS
- [x] Android
- [ ] Web

### Notes / Comments

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.