Solutions to Oom exceptions to applications in the Android platform _android

Source: Internet
Author: User

On the Android platform, the application Oom exception is always a matter of concern. This is usually one of the key points in the program. I will make a brief introduction to how to solve the oom.

First, the oom is a memory overflow, that is, out of Memory. This means that the memory footprint exceeds the maximum assigned by the VM.

How to solve oom, usually oom occurs when you need to use a lot of memory (create or parse bitmap, allocate a large array, etc.), in such a case, there may be oom, as far as I know, most oom is because bitmap is too big. So, here I specifically focus on how to solve the bitmap oom. In fact, the most issued is only to load the visible range of bitmap, imagine such a situation, in the GridView or ListView, the data volume of 5000, each screen shows only 20 elements, so invisible, we do not need to save bitmap in the inner. So we just keep the visible bitmap in memory, and the Invisible ones are released. When the elements slip out, then load the bitmap.

Here I have two ways to avoid oom.

First, the active release of bitmap memory
this way I simply say, not very recommended, this is my first use of a method, but finally proved that it is not the best. (Not recommended)

Its essential idea is:
1, only loading visible area of the bitmap

2. Do not load when sliding

3, stop sliding (Idle), start reloading the visible area of the picture

4. Release the bitmap within the visible region.

It is more complex:
1, we need to monitor gridview/listview sliding events, this is very simple to do, Abslistview#setonscrolllistener (Onscrolllistener L)

2, the active call Bitmap#recycle () method, it will cause a problem, must determine whether this bitmap is referenced by a view (ImageView, etc.), if referenced, we can not simply call the Recycle () method, which will cause an exception, says that the view uses a bitmap that has been recycled.

3, we have to design our own thread to control the start/pause, because the sliding state of the gridview/listview may constantly change, that is, sliding-> stop-> sliding, this state may be constantly changing, which will cause the run in our thread () The logic in the method is more complex, and once it is complicated, the problem may be more.

Based on the above points, this method is not the best, so it is not recommended.

Second, the design cache
This way, I think is a better one, it first took advantage of the cache, I think the cache is a very important thing, the memory of the bitmap alone in a place to manage, this place is cache, its capacity is certain, we may continue to add elements to this cache , you may also remove elements continuously.

In order to better illustrate this way, first of all to introduce the LRUCache.

LruCache
1, this is actually a linkedhashmap, any time, when a value is accessed, it will be moved to the beginning of the queue, so this is why to use the linkedhashmap reason, because to frequently do mobile operations, in order to improve performance, So you have to use Linkedhashmap. When the cache is full, then add a value to the cache, then the end of the queue value will be removed from the queue, this value may be collected by GC.

2, if we want to actively free memory, it is also possible, we can rewrite the entryremoved (Boolean, K, V, v) method.

3, this class is thread-safe, use this class under multithreading, there is no problem.

Synchronized (cache) { 
   if (cache.get (key) = null) { 
     cache.put (key, value); 
  }} 

4, LRUCache APILevel is 12, that is to say, we can not use the SDK 2.3.x below, but it does not matter, LRUCache source code is not complicated, we can directly copy it to their own engineering directory on it.
asynctask<>
This class is also a very important and very common class. It encapsulates thread and handler, and we use it more conveniently, without paying attention to the handler, we know that the UI cannot be updated in the background thread, and in many cases, we usually update the UI after a background thread finishes something. The general practice is to update the UI by sending a message to the handler associated with the UI thread and processing the message inside the handler. After using the Asynctask, we don't have to pay attention to handler. This class has several important methods:

1), OnPreExecute (): Called in the UI thread, which is called immediately after the task is executed. We usually use this method to create a task, such as displaying a wait dialog box to notify the user.

2), Doinbackground (Params ...) : This method from the name can be seen, it is running in the background thread, in this method, to do time-consuming things, such as downloading access to the network, operating files and so on. In this method, we can call the Publishprogress (Progress ...) To invoke the progress of the current task, after invoking this method, the corresponding onprogressupdate (Progress ...) method is invoked, and this method is run on the UI thread.

3), Onprogressupdate (Progress ...) : Run on the UI thread after calling the Publishprogress () method. This method is used to display any form of progress on the UI, for example, you can display a wait dialog box, a log in text form, and a Toast dialog box to display.

4), OnPostExecute (Result): When the task ends, it runs on the UI thread when called.

5, cancel a task, we can call Cancel (Boolean) at any time to cancel a task, when the Cancel () method is invoked, the oncancelled (object) method is called, OnPostExecute (object) method is not invoked, in the Doinbackground (object[]) method, we can use the iscancelled () method to check whether the task is canceled.

6), some rules

The Asynctask instance must be created in the UI thread
Execute (Params ...) Method must be called in the UI thread.
You do not have to manually invoke OnPreExecute (), OnPostExecute (), Doinbackground (), Onprogressupdate () method.
A task can only be executed once.
The general idea
1, always from the cache to take bitmap, if taken to bitmap, directly to the bitmap set to ImageView above.

2. If the cache does not exist, start a task to load (possibly from the file or from the network).

3, each imageview may bind a task, so the ImageView must provide a way to get the task associated with it, why do this? Because before you bind a task to a imageview, you must cancel the original task.

The above is to solve the application Oom exception method, hope for everyone's learning help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.