Android Memory Issues

Source: Internet
Author: User

Learn from: Big Corn Blog (http://blog.csdn.net/a396901990)

I. Memory overflow (oom--out of Memories)

(1) Problems caused by memory overflow show:

1. Slow response time (the JVM virtual opportunity triggers the GC frequently when memory consumption is high)

2. Inexplicable disappearance (when your program takes up more memory, it is more likely to get killed in the background.) Conversely, the smaller the memory footprint, the longer it will exist in the background)

3. Direct Crash (OutOfMemoryError)

(2) Reasons for memory overflow

Because the app has a maximum memory limit per process or per virtual machine, the system throws an oom error if the requested memory resource exceeds this limit. Oom does not have much to do with the rest of the device's memory, which is related to the system running memory. For example, a virtual machine with an earlier Android system has up to 16M of memory, and when an app is launched, the virtual machine keeps requesting memory resources to load the image, and oom occurs when the memory limit is exceeded.

Android Memory consists of:

The app memory consists of 2 parts of Dalvik memory and native memory , Dalvik is the Java heap, and the object created here is assigned here, while native is The way the memory is applied, bitmap is allocated in this way. (After android3.0, the system is assigned by default through Dalvik, and native is managed as a heap). These 2 parts add up to no more than Android's memory limit on a single process, virtual machine.

(3) Why the Android system should set the memory limit of the app

1. Forcing developers to allocate memory more rationally. Limit the available memory limit per app, and you can place some applications that use excessive memory maliciously or unintentionally. Cause other apps to not function properly. Android is a multi-process, and if a process (or an application) consumes too much memory, other apps will not run. Because of the limitations, developers must make good use of limited resources to optimize the use of resources.

2. The screen display has limited content. Even if tens of thousands of images of thousands of data need to be used, but at specific times need to show to the user is always limited, because the screen is so large, the information on the above can be very limited. Most of the information is in a ready-to-display state, so there is no need to give too much heap memory. In other words , the phenomenon of oom, the majority of reasons are our program design problems, need to optimize . Many optimization methods, such as through time to change space, constantly loading the pictures to be used, keep recycling the pictures, the big picture to fit the screen size of mobile phone pictures.

3.davlik Virtual machine design. Apps on Android run with a standalone virtual machine, opening at least one independent virtual machine for each application. This avoids the crash of the virtual machine and causes the whole system to crash, and at the cost of more memory to be wasted. This design ensures the stability of Android.

Two. GC Automatically reclaims memory

The Android GC will recycle the memory resources used by the program according to the specific algorithm, avoid the application's memory request to accumulate more, but the resources that the GC generally recycles are those that have no main object memory or soft reference resource, or more soft reference resource.

(1) Advantages:

1. Reduce programming burden and improve efficiency

C, Memory Recycling in C + + is the programmer manual operation, otherwise the resources will be consumed, resulting in the waste of resources and even collapse. But manual recycling of memory is often a complex and arduous task. The Java language uses a dynamic storage management technology, which automatically frees objects that are no longer referenced by the program, and implements the function of automatic resource recovery according to the specific garbage collection algorithm. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is later used by the new object.

2. Eliminate heap Memory control fragmentation

The memory is fragmented because the object being created and the garbage collector frees up the memory space that the discarded objects occupy. Fragmentation is a free memory hole between the blocks of memory allocated to an object. Defragmentation moves the occupied heap memory to one end of the heap, and the JVM allocates the compiled memory to the new object.

(2) Disadvantages

resource-intensive, unpredictable, unpredictable, non-operational. The university teacher said a very vivid words, "only pull not clean up." This is a very bad habit, in the programming process, or should have a certain degree of programming literacy. What is used and destroyed in time when it is not needed.

Three. View app memory Allocations

1. Monitor the memory situation through the heap tab in DDMS:

In the middle of the heap view is an object called data object, which is a large number of class types in our program.

In the data object row, there is a column "total Size", whose value is the amount of memory for all Java data Objects in the current process. If there is a case in your code that does not release an object reference, the "total Size" value of data object will not have a drop in the beauty line after each GC. As the number of operations increases, the value of "total Size" becomes larger. Until an upper limit is reached, the process is killed.

2. In the app we can pass TotalMemory and freememory

3.ADB Shell Dumpsys Meminfo Com.android.demo

Not finished, to be continued ...

Android Memory Issues

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.