Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation problems with android application

I have an android application that performs image analysis, which is managed with an IntentService - the process takes a couple of seconds each time and works accurately and quickly.

But when the process is repeated in the app around 50 times (as illustrated) it begins to get very slow until the point in which the app and device becomes unusable. When the device is restarted and app opens again it runs as usual.

Inspecting with Android Studio I can see that each time I run the analysis that the memory allocation for the app goes up and up every time by around 1MB. So it is clearly running out of memory when it crashes.

I have used this flag on finishing the analysis and going to the result to try fix background activities;

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

which has had minimal effect, and I understand the IntentService manages itself shutting down. So not sure what else I can do to try and reduce the memory allocation or at least clear down allocation and stop adding to it?

Image Analysis Workflow

Further details:

  • The application is using the camera implementation based on Google Camera2
  • The analysis is done with a C++ library through the IntentService
like image 905
Stacker-flow Avatar asked Feb 05 '17 16:02

Stacker-flow


People also ask

How do I fix out of memory error on Android?

If you haven't seen any OOM in your Android application, then you are going to have one in future. The OutOfMemoryError comes in Android due to memory leaks. So, in order to remove the OutOfMemoryError, you need to remove memory leaks from your Android application.

Does memory related issues can cause application crash?

Your application can be terminated because other applications are consuming too much memory, and if your application is doing any work at the time (backup, server-sync, cache pruning, etc.) you may end up with a crash report that doesn't appear to be memory related.


2 Answers

It seems you are not handling the resources (variables, image files,etc) properly, and its creating memory leaks in your application.

you can find here in this blog Written by Johan on handling the memory leaks in your application or see this SO Question.

Avoid memory leaks on Android

If the memory leaks are being generated in the c++ library then you can easily find the resource which is leaking the memory in debug mode.

After the result activity you should call the garbage collector as suggested by Grisgram and close any unused resources.

It would be good if you could provide the stack trace in the question.

like image 159
smali Avatar answered Sep 19 '22 02:09

smali


Try using leakCanary https://github.com/square/leakcanary to find out what is causing the leak and use a weakReference https://developer.android.com/reference/java/lang/ref/WeakReference.html to allow it to be garbage collected when necessary. It may also be that the device you are using does not have enough memory to hold 50 high res images in memory at the same time. You could try lowering the resolution of the images if you are keeping them in memory and be sure you are recycling bitmaps https://developer.android.com/topic/performance/graphics/manage-memory.html

I would also consider using a threadPoolExecutor instead of an intent service, they are much more configurable https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html

like image 22
SBC Avatar answered Sep 19 '22 02:09

SBC