Android development: memory mechanism analysis-heap and stack

Source: Internet
Author: User

Android development: memory mechanism analysis-heap and stack

1. Heap and Stack of dalvik

Here we only talk about the memory of the dalvik java part. In fact, besides the dalvik part, there is also native. Let's talk about this later.

The following describes the data types listed above. Only by understanding where the data we applied is can we better control our own programs.

 2. object instance data

It is actually a method to save the attributes of the object instance, the type of the property, and the type tag of the object itself, but not the instance. The instance method is a data instruction, which is saved in the Stack, that is, the class method in the table above.

After the object instance is allocated in Heap, a 4-byte Heap memory address is saved in the stack to find the instance of the object. Because Heap instances are used in the Stack, especially when calling an instance, a this pointer is required.

3. Internal method Variables

The internal variables of the class method are classified into two types: simple type stored in the Stack; object type saved in the Stack address and Heap saved value.

 4. Non-static and static methods

The non-static method has an implicit input parameter, which is passed by the dalvik virtual machine. This implicit parameter is the address pointer of the object instance in the Stack. Therefore, non-static methods (instruction code in Stack) can always find their own private data (Object attribute values in Heap ). Of course, non-static methods must also obtain this implicit parameter. Therefore, before calling a non-static method, you must first create an object instance to obtain the address pointer in the Stack, otherwise, the dalvik virtual machine cannot pass implicit parameters to non-static methods.

Static methods do not have implicit parameters, so they do not need new objects. As long as the class file is loaded into the JVM Stack by ClassLoader, the static method can be called. Therefore, we can directly call the class method using the class name. Of course, the static method cannot access the object attributes in Heap at this time.

 5. static and dynamic attributes

Static attributes are stored in the Stack, but dynamic attributes are stored in the Heap. Because all commands and data in the Stack are fixed, it is easy to calculate the offset, so the class method (static and non-static) can access the static attributes of the class. Because static attributes are stored in the Stack, they have global attributes.

 6. Summary

The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.

The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, int, short, long, byte, float, double, boolean, char) and object handles.

The above analysis shows that the general principle of Java Processing Heap and Stack is the same as that of C ++. There is only an additional memory recycle mechanism, so that programmers do not have to call delete to release the memory. Just like in C ++, the memory applied for by new is usually put in the heap, and the general temporary variables are put in the stack.

Today, we mainly talk about the difference between stack and stack in the dalvik of Android and what data to store. In addition to the dalvik memory, Android also has a native Memory concept.

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.