Android out of memory

Source: Internet
Author: User

1. cursor is not disabled when a large number of databases are queried.

Incorrect syntax:

Cursor cursor = getcontentresolver (). Query ();

If (cursor! = NULL)

{

Cursor. movetofirst ();

While (! Cursor. isafterlast ())

{

..............

}

}

Correct syntax:

Cursor cursor = getcontentresolver (). Query ();

If (cursor! = NULL)

{

Cursor. movetofirst ();

While (! Cursor. isafterlast ())

{

..............

}

 

Cursor. close;

Cursor = NULL;

}

2. Bitmap objects are not recycled in time

Because bitmap objects occupy memory, it is best to use bitmap. recyle () to reclaim the memory occupied by bitmap objects after the bitmap objects are used up.

3. convertview in the cache is not used in the adapter

Incorrect syntax:

Pubic view getview (INT position, view convertview, viewgroup parent)

{

Layoutinflater Inflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );

View view = Inflater. Inflate (R. layout. listview_item_manage_bookshelves, null );

........................

}

Correct syntax:

Pubic view getview (INT position, view convertview, viewgroup parent)

{

View view;

If (convertview = NULL)

{

Layoutinflater Inflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );

View = Inflater. Inflate (R. layout. listview_item_manage_bookshelves, null );

}

Else

View = convertview;

........................

}

4. Reclaim resources based on the activity Lifecycle

In the onstop () or ondestroy () method, some methods and objects are recycled, for example:

If (marrarylist! = NULL)

Marraylist = NULL;

......................

.......................

System. GC ();
System. GC ();

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.