Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>ok then @sherays Especially in Your case, If you execute another request to the server while previous one is not finished yet (in case of similar requests), this is a chance of memory leak.</p> <p>Actually, there shouldn't be any issue with <code>AsyncTask</code> recycling unless You hold any reference to it from param or generate memory leaks inside <code>doInBackground().</code></p> <p>So, You might think, that if You're creating many long-running AsyncTasks then it would lead to some memory issues. Actually it's not true (at least on latest Android versions). AsyncTask source code shows that</p> <p>It uses singleton bounded executor:</p> <pre><code>private static final int CORE_POOL_SIZE = 5; private static final int MAXIMUM_POOL_SIZE = 128; private static final int KEEP_ALIVE = 1; public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory); </code></pre> <p>That means that executor won't run more than 128 AsyncTasks the same time (128 is not very big per my understanding).</p> <p>It uses bounded query for the Executor:</p> <pre><code>private static final BlockingQueue&lt;Runnable&gt; sPoolWorkQueue = new LinkedBlockingQueue&lt;Runnable&gt;(10); </code></pre> <p>So, based on the above points, number of created and running the same time AsyncTasks are limited and not so big. So, if Your code inside AsyncTask doesn't create any memory leaks, then per my understanding there's no issue. Same time Android won't let You spam itself with AsyncTasks. Checkout <code>ThreadPoolExecutors</code> description to get familiar with the way it manages a memory (If You worry about too many created threads the same time).</p> <p>so still,if you face memory leak then cancel the task:</p> <p>Regarding <code>cancel()</code> call, based on Android documentation for AsyncTask:</p> <p>Cancelling a task</p> <p>A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, <code>onCancelled(Object)</code>, instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of <code>isCancelled()</code> periodically from <code>doInBackground(Object[])</code>, if possible (inside a loop for instance.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload