Memory leaks in Android

Source: Internet
Author: User

Android program is mainly written by Java code, we all know that Java in memory recycling has its own system, do not need coder to do memory destruction, but in the programming time there is a memory leak occurs, this is generally because of what?

> First of all, let's be clear about how the Java Memory Auto-recycle mechanism works: in Java, The allocation of memory is done by the program, and the release of the memory is done by the garbage collector (garbage COLLECTION,GC), and the programmer does not need to call the function to free memory, but it can only reclaim space occupied by those objects that are useless and are no longer referenced by other objects .
principle of releasing the object is that the object is no longer referenced . Typically, GC threads have a lower priority level. There are a number of policies that the JVM calls the GC, some of which are used to a certain extent, the GC starts to work, there are timed executions, there is a gentle execution of the GC, and some interrupt-execution GC.
so when the GC cannot tell if an object is no longer referenced, it will cause a memory leak. so under what circumstances does the GC not determine whether an object is no longer referenced? These objects have the following two characteristics, first of all, these objects are referenced, that is, in the direction of the tree diagram, there is a branch path can be connected to it, and secondly, these objects are useless, that is, the program will no longer use these objects. If the object satisfies both conditions, these objects can be judged as a memory leak in Java, which is not reclaimed by the GC, but it consumes memory.

1. the cursor of the database is not closed
2. When constructing adapter, the cache is not used Contentview
The optimization problem of the derived ListView-----Reduce the objects that create the view, fully use Contentview, you can use a static class to optimize the process of processing GetView
3.Bitmap object uses recycle () to free memory when not in use
Objects in 4.activity have a life cycle greater than activity

Memory leaks in Android

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.