Android solves memory overflow problems

Source: Internet
Author: User

Android solves memory overflow problems
 



1. When the project contains a large number of images or the picture is too large
Method 1: proportional reduction of images
  1. BitmapFactory. Options options = new BitmapFactory. Options ();
  2. Options. inSampleSize = 4
  3. Copy code Method 2: Use soft references to images and perform recyle () operations in a timely manner
    1.  
    2. SoftReference Bitmap;
    3. Bitmap = new SoftReference (PBitmap );
    4. If (bitmap! = Null ){
    5. If (bitmap. get ()! = Null &&! Bitmap. get (). isRecycled ()){
    6. Bitmap. get (). recycle ();
    7. Bitmap = null;
    8. }
    9. }
    10. Copy code method 3: Design and code the complex listview properly (I personally think this is a reliable point) 1. Pay attention to reuse convertView In the Adapter and the use of the holder Mechanism
      ----- Reference: api demo list 14. Efficient Adapter

      2. The above method has not been successfully tried. lazy loading data can be used.
      ----- Reference: api demo list 13
      1. Public View getView (int position, View convertView, ViewGroup parent ){
      2. If (convertView = null ){
      3. V = mInflater. inflate (resource, parent, false );
      4. Final int [] to = mTo;
      5. Final int count = to. length;
      6. Final View [] holder = new View [count];
      7. For (int I = 0; I <count; I ++ ){
      8. Holder [I] = v. findViewById (to [I]);
      9. }
      10. V. setTag (holder);} else {
      11. }
      12. }
      13. Code Copying Method 4: OOM after switching N times on a single page
        1. check whether there are large images in the page layout, such as background images. Remove the settings in xml, and set the background image in the Program (in the onCreate () method ):
        1. Drawable bg = getResources (). getDrawable (R. drawable. bg );
        2. XXX. setBackgroundDrawable (rlAdDetailone_bg );
        3. When copying code in Activity destory, note that bg. setCallback (null); prevent the Activity from being released in time.
          2. Similar to the above method, directly load the xml configuration file into a view and put it in a container, and then directly call this. setContentView (View view) to avoid repeated xml loading.

          Method 5: Use as few code as possible during page switching. For example, repeatedly calling the database and repeatedly using some objects .....

          Method 6: the Android heap memory can also be customized and the memory of the Dalvik virtual machine can be optimized.
          -- Reference: http://blog.csdn.net/wenhaiyan/article/details/5519567

          • Note: If this method is used, only version <= 2.2 can be selected for project build target. Otherwise, compilation will fail. Therefore, this method is not recommended.
            1. Private final static int CWJ_HEAP_SIZE = 6*1024*1024;
            2. Private final static float TARGET_HEAP_UTILIZATION = 0.75f;
            3. VMRuntime. getRuntime (). setMinimumHeapSize (CWJ_HEAP_SIZE );
            4. VMRuntime. getRuntime (). setTargetHeapUtilization (TARGET_HEAP_UTILIZATION );
            5. Copy code
 

Related Article

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.