Memory allocation problems with android application : <p>I have an android application that performs image analysis, which is managed with an <code>IntentService</code> - the process takes a couple of seconds each time and works accurately and quickly. </p>

<p>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.</p>

<p>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 <em>1MB</em>. So it is clearly running out of memory when it crashes.</p>

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

<pre><code>intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
</code></pre>

<p>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?</p>

<p><a href="https://i.stack.imgur.com/S12aw.png"><img src="https://i.stack.imgur.com/S12aw.png" alt="Image Analysis Workflow"></a></p>

<p>Further details:</p>

<ul>
<li>The application is using the camera implementation based on <a href="https://github.com/googlesamples/android-Camera2Basic">Google Camera2</a></li>
<li>The analysis is done with a C++ library through the IntentService</li>
</ul>
