Android memory analysis [management, mechanism, analysis]

Source: Internet
Author: User

There are a lot of content in the article, which can be divided into development. This article first describes the theory, and the next article describes the details of Android attention.

I. Android memory mechanism

Android programs are written in Java, so the memory management of Android is similar to that of Java. The programmer allocates memory for the object through new, and all objects are allocated space in the Java heap. However, the release of the object is done by the garbage collector. In C/C ++, the memory mechanism is "Who is polluted and who is responsible for governance". Java is more user-friendly and a dedicated garbage collector (GC) is invited for us ).

2. What is GC? Why does GC exist?
GC is the meaning of garbage collection (gabage collection). Memory Processing is a place where programmers are prone to problems. Forgetting or wrong memory collection can lead to instability or even crash of programs or systems, the GC function provided by Java can automatically monitor whether the object exceeded the scope to automatically recycle the memory. the Java language does not provide a display operation to release the allocated memory.

Iii. Advantages and principles of garbage collection. Two recovery mechanisms are also considered.
A notable feature of Java is the introduction of the garbage collection mechanism, which helps C ++ programmers solve the most troublesome memory management problems, it makes memory management unnecessary for Java programmers when writing programs. Because of the garbage collection mechanism, objects in Java do not have the "Scope" concept, and only objects can be referenced with "Scope ". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector is usually used as a separate low-level thread to clear and recycle objects that have been killed in the memory heap or are not used for a long time, programmers cannot call the Garbage Collector to recycle an object or all objects in real time. The collection mechanism involves generational replication, garbage collection, marking, and incremental garbage collection.

4. What is the basic principle of the garbage collector? Can the Garbage Collector recycle memory immediately? Is there any way to proactively notify virtual machines to recycle garbage?
For GC, when a programmer creates an object, GC starts to monitor the address, size, and usage of the object. Generally, GC records and manages all objects in heap by Directed Graphs. This method is used to determine which objects are "reachable" and which objects are "inaccessible ". When GC determines that some objects are "inaccessible", GC has the responsibility to recycle the memory space. Yes. Programmers can manually execute system. GC () to notify the GC to run, but the Java language specification does not guarantee that GC will be executed.
 
5. Under what circumstances will GC leak in the Android system? Video Codec/memory leakage
The main cause of Memory leakage is that I forgot to release the memory I previously applied. If a reference to useless objects exists in the program, these objects will reside in the memory and consume the memory, because GC cannot be performed by the garbage collector to verify whether these objects are no longer needed. If an object is referenced, the object is defined as "valid activity" and will not be released. To determine that the memory occupied by an object will be recycled, make sure that the object will no longer be used. A typical practice is to set the object data member to null or remove the object from the set. However, when local variables are not needed, you do not need to explicitly set them to null because these references are automatically cleared when a method is executed.

Why is memory leakage caused by Java garbage collection? Example:

Vector v = new vector (10); For (INT I = 1; I <100; I ++) {object o = new object (); V. add (o); O = NULL;} // at this time, all object objects are not released because variable v references these objects.

The root cause of Java Memory leakage is that it saves reference to variable types that cannot be accessed again.

6. Android memory overflow
How does Android memory overflow occur?
The android virtual machine is a register-based Dalvik. Its maximum heap size is generally 16 m, and some machines are 24 m. That is to say, the memory space we can use is limited. If our memory usage exceeds a certain level, an outofmemory error occurs.
Why is memory insufficient? I think there are two main reasons:
Due to program errors, some resources (such as context) can be referenced for a long time, resulting in Memory leakage and unreleased resources. Saves multiple objects (such as Bitmap) that consume too much memory, resulting in memory exceeding the limit.

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.