How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?

DoDo

I've found a memory leak in Android 5.x Camera2 API which I also reported. The problem is when you use Android Lollipop device that has Camera2 API implemented in LEGACY mode. On such devices, calling context.getSystemService(Context.CAMERA_SERVICE) causes context to be retained so that it won't be garbage collected.

If this context is your activity that is started multiple times, you can end up with hanging references to dozens of instances of your activity that are never garbage collected.

The issue appears to happen only on Lollipop devices that have Camera2 API implemented in LEGACY mode (e.g. HTC One M8, Samsung Galaxy S4), while it does not happen on Samsung Galaxy S6 which implements Camera2 API in FULL mode.

To demonstrate the issue, I've created a small demo app. The app contains two activities: first that contains a button which calls the second activity. The second activity obtains the CameraManager and queries the level of Camera2 API support for first back-facing camera and returns the result to first activity. If you run the app on device that implements Camera2 API in LEGACY mode, after tapping the button 98 times, causing GC and then dumping HPROF you will see exactly 98 live instances of Main2Activity, like this http://www.pohrani.com/f/1H/gs/4EFlHKoj/sgs4.png

If you do the same on device that implements Camera2 API in FULL mode, you will see 0 live instances of Main2Activity, like this http://www.pohrani.com/f/2q/bV/4srUZIJL/sgs6.png

Is there a way to workaround this leak?

One might ask why am I doing this? At our company, we are developing barcode and OCR scanning solutions and also a famous PhotoMath app. So we have a scan activity that controls the camera and scanning process. While starting up, activity checks if device supports Camera2 API in either FULL or LIMITED mode and attempts to use it for better performance, whilst if Camera2 API is in LEGACY mode, then we prefer using camera management using old camera API, as we do on pre-Lollipop devices.

Because of the mentioned memory leak, whenever a client that has integrated our SDK into its app starts the scan activity, performs a scan and obtains the result, one instance of scan activity will be leaked due to the bug. If client scans a lot, this can eat up a more than 20 MB of memory - a serious issue!

So if someone knows how to make a workaround for this issue, I'll be eternally grateful!

CommonsWare

Is there a way to workaround this leak?

You could call getSystemService() on the Application singleton. So, instead of:

getSystemService(CAMERA_SERVICE)

you would use:

getApplicationContext().getSystemService(CAMERA_SERVICE)

If your assessment is correct, then this will cause those additional references to be to the existing Application singleton, which is always around in your process. It's effectively "pre-leaked", and you cannot leak it further by having more references to it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid this memory leak?

From Dev

How to release memory in android to avoid memory leak

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How can I fix to avoid a memory leak?

From Dev

How can I avoid a memory leak in this function?

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

Avoid Memory leak

From Dev

How to avoid memory leak with shared_ptr and SWIG

From Dev

How to avoid memory leak at design-implementation level

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

How to avoid a memory leak by using pthread_cancel?

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

How to handle unmounting a component with set interval to avoid a memory leak

From Dev

Avoid memory leak with WeakReference Android

From Dev

Android inner classes memory leak and leak by context?

From Dev

How to use a class with a context argument in a static context without causing a memory leak?

From Dev

Android: How to avoid service connection leak when bound service is destroyed

From Dev

Memory leak - Service + thread

From Dev

How to block this memory leak?

From Dev

How to detect a memory leak?

From Dev

nodejs setMaxListeners to avoid memory leak detection

From Dev

Ways to avoid memory leak when exception thrown

From Dev

weak to non class types to avoid memory leak

From Dev

Is setting ulimit -v sufficient to avoid memory leak

From Dev

WPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak

From Dev

How to avoid memory leak when passing function-returned pointer as input to other function?

From Dev

Android camera Bitmap memory leak

From Dev

How to prevent memory leak in code

Related Related

  1. 1

    How to avoid this memory leak?

  2. 2

    How to release memory in android to avoid memory leak

  3. 3

    How to avoid a memory leak with __block and completion blocks

  4. 4

    how to avoid memory leak in dynamically allocated widget

  5. 5

    How can I fix to avoid a memory leak?

  6. 6

    How can I avoid a memory leak in this function?

  7. 7

    How to avoid a memory leak with __block and completion blocks

  8. 8

    Avoid Memory leak

  9. 9

    How to avoid memory leak with shared_ptr and SWIG

  10. 10

    How to avoid memory leak at design-implementation level

  11. 11

    Node and RxJs: How can I avoid a memory leak on a long process?

  12. 12

    How to avoid a memory leak by using pthread_cancel?

  13. 13

    Node and RxJs: How can I avoid a memory leak on a long process?

  14. 14

    How to handle unmounting a component with set interval to avoid a memory leak

  15. 15

    Avoid memory leak with WeakReference Android

  16. 16

    Android inner classes memory leak and leak by context?

  17. 17

    How to use a class with a context argument in a static context without causing a memory leak?

  18. 18

    Android: How to avoid service connection leak when bound service is destroyed

  19. 19

    Memory leak - Service + thread

  20. 20

    How to block this memory leak?

  21. 21

    How to detect a memory leak?

  22. 22

    nodejs setMaxListeners to avoid memory leak detection

  23. 23

    Ways to avoid memory leak when exception thrown

  24. 24

    weak to non class types to avoid memory leak

  25. 25

    Is setting ulimit -v sufficient to avoid memory leak

  26. 26

    WPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak

  27. 27

    How to avoid memory leak when passing function-returned pointer as input to other function?

  28. 28

    Android camera Bitmap memory leak

  29. 29

    How to prevent memory leak in code

HotTag

Archive