Android Memory Management Analysis

Source: Internet
Author: User

 

Many developers come from j2-based or j2ee and do not have a good understanding of memory usage. The Android Development Network provides some architectural guidance for you, prevent the occurrence of the bean curd residue project. Android, as an intelligent Platform Based on Java language, is required to understand the memory management mechanism of Android programs for developing high-performance and quality software.
There is no major difference between the Dalvik VM of Android and sun jvm in terms of the basic aspect. It is only the optimization of bytecode. We need to know when to use gc, when to use recycle, and whether to use finalization, because Java only needs to release memory that new developers do not need to display for memory allocation, but this leads to a higher chance of Memory leakage.

1. For general developers, you need to understand the four reference methods of Java, such as strong reference, soft reference, weak reference, and virtual reference. Some Complex programs may encounter exceptions similar to OutOfMemoryError during long-term operation.

2. don't count too much on gc. the unused objects can be displayed as null, for example, obj = null. Here we are prompted that java gc uses a directed graph, determining whether an object is valid depends on whether other objects can reach the vertex of this object. The overhead of a directed graph is imaginable relative to a linked list or binary tree.

3. android can use the totalMemory () freeMemory () method of the Runtime class to obtain some memory information of the VM. For the heap memory of the system, you can use Dalvik. the getMinimumHeapSize () method of the VMRuntime class obtains the minimum available heap memory. It also shows that releasing soft references can call the gcSoftReferences () method of the class to obtain more running memory.

4. For multi-threaded processing, if there are many concurrent threads and frequent creation and release at the same time, you can solve the efficiency bottleneck of thread creation through the concurrent thread pool.

5. Do not create too many local variables in the loop.

In many cases, we need to consider the memory management issue on the Android platform. The Dalvik VM allocates a certain amount of available heap memory to each process, when we handle resource-consuming operations, we may encounter exceptions such as the OOM error (OutOfMemoryError). We have observed the domestic design similar to the Market client, basically, there is no good memory management mechanism or cache processing.

If careful netizens may find that the icons of each list item are asynchronously refreshed during loading on the Android Market client, but when we scroll down to a certain number, for example, 50, when we scroll back, we may see some App icons and re-load them. Of course, this process may be cached from the SQLite database, however, the memory has been managed in a way similar to SoftReference.

In Java memory management, there are four categories of references: strong reference HardReference, weak reference WeakReference, soft reference SoftReference, and Virtual Reference PhantomReference. Their differences are also obvious. Even if the VM memory is too tight to throw out OOM, the referenced objects will not be recycled, and WeakReference is more suitable for a small number of objects, however, objects with a relatively large volume are the most vulnerable to garbage collection among the four references, however, when displaying App icons for apps similar to those in the Android Market, we can consider using SoftReference to solve the problem that memory will not be quickly recycled. At the same time, when the memory shortage faces Java
When the VM crashes and throws OOM, the soft reference will forcibly recycle the memory. The final Virtual Reference is generally meaningless and only observes the GC activity status, the test is more practical and must be used with ReferenceQueue.

For a group of data, we can add a group of SoftReference objects through HashMap to temporarily retain some data, and for the content that needs to be retrieved through the network repeatedly, cache can be stored through a local file system or database.

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.