Android phone to the application allocated memory is usually about 8 trillion, if the processing of improper memory processing is easy to cause outofmemoryerror, our products have the most error is outofmemoryerror exception,
In solving this anomaly, I found a lot of information about OutOfMemoryError on the Internet.
OutOfMemoryError is mainly caused by the following situations:
1. The cursor of the database is not closed.
When you manipulate the SQLite database, the cursor is a collection of each row in the database table, and the cursor provides many ways to easily read the values in the database.
You can get the values in the database based on index, column name, and so on, and you can call MoveToNext () to move to the next line by using a cursor
When we're done with the database, be sure to call Close () of the cursor object to turn off the cursor and release the resource.
2. The construction adapter does not use the cache contentview.
When inheriting Baseadapter, let's rewrite getview (int position, View Convertview, ViewGroup Parent) method,
The second parameter, Convertview, is the reusable object we're going to use.
Java code
- @Override
- Public View GetView (int position, View Convertview, ViewGroup parent) {
- Viewholder vholder = null;
- //If the Convertview object is empty, a new object is created, not empty, and reused
- if (Convertview = = null) {
- Convertview = Inflater.inflate (..., null);
- //Create Viewhodler Object
- Vholder = new Viewholder ();
- Vholder.img= (ImageView) Convertview.findviewbyid (...);
- vholder.tv= (TextView) Convertview
- . Findviewbyid (...);
- //Save Viewhodler to tag
- Convertview.settag (Vholder);
- } Else {
- //When the Convertview is not empty, the view is obtained by Gettag ()
- Vholder = (Viewholder) convertview.gettag ();
- }
- //Assign a value to the object to modify the displayed value
- VHolder.img.setImageBitmap (...);
- VHolder.tv.setText (...);
- return convertview;
- }
- //Package the displayed view into a class
- Static class Viewholder {
- TextView TV;
- ImageView img;
- }
Here only the use of methods, specific performance test article see:
How GetView in the ListView + How to place multiple item in the ListView
Http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html
Android development for the ListView adapter (Adapter) optimization
http://shinfocom.iteye.com/blog/1231511
3. Unregisterreceiver () is not called after calling Registerreceiver ().
Broadcast receivers (Broadcastreceiver) are often used in applications, can send broadcast notification UI updates after multi-threaded tasks are completed, and can receive system broadcasts to implement some functions
can be registered by code:
Intentfilter postfilter = new Intentfilter ();
Postfilter.addaction (Getpackagename () + ". Background.job");
This.registerreceiver (receiver, postfilter);
When we use the Registerreceiver () method in our activity to register the Broadcastreceiver, be sure to call the Unregisterreceiver () method during the activity's lifetime to unregister
This means that the registerreceiver () and Unregisterreceiver () methods must appear in pairs, and usually we can rewrite the activity's Ondestory () method:
Java code
- @Override
- protected void OnDestroy () {
- This.unregisterreceiver (receiver);
- Super.ondestroy ();
- }
4. Inputstream/outputstream is not turned off.
This is not much to say, we finished the input and output stream to close the flow
Recycle () was not called after 5.Bitmap was used.
Bad image processing is another number one cause of memory overflow, (also reflected in our products),
After we have finished processing the picture, we can reclaim the picture object by calling the Recycle () method.
Java code
- if (!bitmap.isrecycled ())
- {
- Bitmap.recycle ()
- }
Besides:
Using ImageView directly shows that bitmap can take up more resources, especially when the picture is large, which can cause crashes.
Using Bitmapfactory.options to set up insamplesize, this reduces the need for system resources.
The property value insamplesize indicates that the thumbnail size is one of a fraction of the original picture size, that is, if the value is 2, the width and height of the thumbnail being taken are 1/2 of the original picture, and the image size is 1/4 of the original size.
Bitmapfactory.options bitmapfactoryoptions = new Bitmapfactory.options ();
Bitmapfactoryoptions.injustdecodebounds = true;
Bitmapfactoryoptions.insamplesize = 2;
Be sure to set it back to false, because we set it to true before
After setting Injustdecodebounds to True, DecodeFile does not allocate space, that is, bitmapfactory decoded bitmap is null, but can calculate the length and width of the original picture
Options.injustdecodebounds = false;
Bitmap bmp = Bitmapfactory.decodefile (Sourcebitmap, Options);
6.Context leakage.
This is a very vague case of outofmemoryerror. Let's look at an example from the Android website:
Java code
- Private static drawable Sbackground;
- @Override
- protected void OnCreate (Bundle state) {
- super.oncreate (state);
- TextView label = new TextView (this);
- Label.settext ("Leaks is bad");
- if (sbackground = = null) {
- Sbackground = getdrawable (R.drawable.large_bitmap);
- }
- Label.setbackgrounddrawable (Sbackground);
- Setcontentview (label);
- }
This code is very efficient, but at the same time it is extremely wrong;
It leaks the activity that was created at the start of the first screen orientation. When a drawable is attached to a view,
View will set it as a callback to drawable. The code snippet above means that drawable has a textview reference,
TextView also has a reference to the activity (context type), in other words, drawable has more object references. Even if the activity is destroyed, the memory is still not released.
In addition, a reference to a context that exceeds its own life cycle can also lead to a context leak. So try to use application as the context type.
This context has the same long life cycle as the application and does not depend on the activity's life cycle. If you are going to save a long time object,
And it needs a Context, remember to use the Application object. You can easily get application objects by calling Context.getapplicationcontext () or activity.getapplication ().
Recently encountered a situation caused a context leak, that is, when the activity is destroyed, there are other threads in the non-stop.
Summarize the issues you should be aware of to avoid a context leak:
1. Use application for this type of context.
2. Note that the reference to the context does not exceed its own life cycle.
3. Use the "static" keyword carefully.
4.Context if the thread, must be in OnDestroy () in time to stop.
7.static Keywords
When a member variable of a class is declared static, it belongs to the class and not to the object, and if we declare a large resource object (Bitmap,context, etc.) as static, then those resources are not recycled as the object is recycled.
Always exists, so be cautious when defining member variables using the static keyword.
Android Memory leak Optimization Summary