Android Basic Learning-memory overflow and leaks

Source: Internet
Author: User

A few days to go to the interview, so tidy up the knowledge points, previously know, but did not think carefully, may be in the interview when the answer is not complete, if you think that in addition to my summary of other circumstances can leave a message, thank you!

What is a memory leak? How to solve?

A memory leak, also known as a "storage leak," is the space created in memory to create an object, is not released after use, or cannot be released, resulting in a space that has not been released. Until the end of the program. This is called a memory leak.

I summarized the following, the reason for the memory leak: objects that are no longer useful are referenced by other objects that are still useful as the main cause of memory leaks.

1 The cursor is not closed after querying the SQLite database.

2 The ListView does not reuse the Convertview when setting the adapter, so each scroll will create a View object.

3 Bitmap object is not in use without calling recycle () to free memory.

A 4 object is referenced by an object that has a long life cycle, resulting in an inability to be freed, such as an activity being referenced by a static collection, and the object cannot be reclaimed even if OnDestroy () is called. (as little as possible in other objects to declare the activity of the object, can use fragment instead of using fragment instead).

5 The registered broadcast is not canceled. (Remember, after we registerreceiver, we must be in the OnDestroy () Unregisterreceiver, this is a set of. )
7 A memory leak caused by an object not cleaned up in the collection. We usually add references to some objects to the collection, and when we don't need the object, we don't clean up its references from the collection, so the collection gets bigger. If the set is static, the situation is even worse.

8 The life cycle of static variables is closely related to classes, which are allocated on the method area, and garbage collection generally does not reclaim this piece of memory. So we use the static object in the code, if we do not need to assign null values, to eliminate the object's reference, then these objects are very difficult to garbage collection, if these objects are more or larger, the program will be more likely to have oom.


Discovery of memory leaks:

1 Use the heap tool in Ddms to find out if there is a memory overflow.

2 Use the Memory Analyzer Tool MAT to find the object of memory leak.


How memory leaks are resolved: (The only solution is to reverse the above situation)
1 in the process of writing the application, for Braodcastreceiver, Contentobserver, Fileobserver must be written off after the activity ondestory or some kind of declaration cycle, Otherwise, the activity class will be strongly referenced by the system and will not be recycled by memory.
2 when defining a member variable, do not directly reference the activity as a member variable. is to define mainactivity m in other activity, so that even if mainactivity exits, the object will not be recycled.
3 Bitmap object is no longer in use, call the Recycle () method to free memory.
4 ListView in Convertview multiplexing

5 try to use the context of application to replace the context associated with activity (it is recommended to write a MyApplication class to inherit application, to store some global variables, to provide context, Handler and other objects)

6 static variables should be =null when not in use;

7 to tamper with the object's reference. (soft reference, weak reference, virtual reference; see: Http://www.cnblogs.com/lwbqqyumidi/p/4151833.html)

Two what is memory overflow? How to solve.

Memory overflow means that the operation uses more memory than the virtual machine's maximum memory, which can cause memory overflow.

The default maximum memory value for Android apps is 16M, (VM heap:16 in the emulator), but different models may be different for different phones. Once this value is exceeded, the Oom exception is thrown, so the memory is kept free.

1 part of the reason is due to a memory leak, memory leaks cause some of the memory is not released, has occupied space, accumulation, may cause memory overflow.

2 Loading a large picture (or possibly a larger other object), causing a memory overflow

Resolution of memory Overflow:

1 try to resolve the memory leak.

2 when loading a large picture, to optimize the picture, optimize the program:

A convert the picture into thumbnails and reload.

b Reduce the memory size of each pixel in the picture, load the picture in arbg_4444 color mode (default argb_8888, 4 byte per pixel, argb_4444 2byte)

3 online has said by optimizing Dalvik virtual machine heap memory (http://blog.csdn.net/t12x3456/article/details/7876668)

Interview when the answer is enough, you can also expand the following, such as talk about Dalvik virtual machine memory, if the interviewer interested, can also pull a garbage collection mechanism.
I think so, you technical God, honestly think there is anything to add, can leave a message, thank you!

Android Basic Learning-memory overflow and leaks

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.