Android memory release processing

Source: Internet
Author: User

Android memory release processing

I don't know what kind of processing is done for the release of the android memory. I am not familiar with android. Recently, during the development of small games, I felt better when I first played games because there were many game interface components, however, after entering the game interface for several times, the game will get stuck and I will be speechless in an instant. If I want to get it, I still cannot release the memory usage. After some learning, although the root cause is not found completely, it has been mitigated a lot. Let's talk about how I handle it here. If you have your own good solutions, I hope you can share them with me, so that everyone can make progress together. The following are some of my practices:

First, I know most of the memory usage (I usually release resources in onDestroy ):

1. image resources;

2. list usage;

3. array occupation;

4. Use of other defined methods;

5. Thread callback. The thread is not closed.

In the face of these problems, I use the following methods:

1. Image Resource Recycling

Private void recycleBitmap ()
{
If (viewGroup! = Null)
{
Int count = viewGroup. getChildCount;
For (int I = 0; I {
View view = viewGroup. getChildAt (I );
ImageView img = (ImageView) view. findViewById (R. id. Image );
If (img! = Null)
{
Drawable drawable = img. getDrawable ();
If (drawable! = Null)
{
If (drawable instanceof BitmapDrawable)
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable. getBitmap ();
If (bitmap! = Null)
Bitmap. recycle ();
}
}
}
}
}
}


2. list usage:
Method: list. clear (); and then empty in the system. I don't know if this is necessary, but there is only one sentence, and there is no exception, so by the way;


3. array occupation:
Method: empty if it is used up;


4. Usage of other defined methods:
Method: Empty directly. If there are some lists or arrays in the method, my solution is to define another method to release resources in the defined method, call the method to release the internal data of the method when you want to release the resource;


5. Thread callback. The thread is not closed:
Method: Remove callback mHandler. removeCallbacks (callback hod );

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.