Android Development: Memory mechanism analysis-heap and stack

Source: Internet
Author: User
Tags garbage collection variables require advantage

1, Dalvik heap and Stack

This is just dalvik the Java part of the memory, in fact, in addition to the Dalvik part, there are native. I'll talk about it later.

The following is a description of the data types listed above, and we can better control our own programs only if we know where the data we are applying is.

 2. Object instance Data

It is actually saving the properties of the object instance, the type of the property, the type tag of the object itself, and so on, but not the method of saving the instance. The method of an instance is a data instruction, which is stored in the stack, which is the class method in the table above.

When an object instance is allocated in heap, a 4-byte heap memory address is saved in the stack to find an instance of the object. Because an instance of heap is used inside a stack, especially when invoking an instance, you need to pass in a this pointer.

3. Method Internal variables

The internal variables of the class method are divided into two cases: the simple type is saved in the stack, the object type holds the address in the stack, and the value is saved in the heap.

 4. Non-static methods and static methods

A non-static method has an implied incoming parameter, which is Dalvik the virtual machine, which 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 property values in heap). Of course non-static methods must also obtain this implied parameter, so a non-static method must first be new to an object instance to get the address pointer in the stack, otherwise the Dalvik virtual machine will not be able to pass the suppressed parameters to the Non-static method.

A static method has no implied parameters and therefore does not require a new object, which can be invoked as long as the class file is ClassLoader load into the stack of the JVM. So we can use the class name directly to invoke the method of the class. Of course, at this point the static method is not accessing the object properties in the heap.

 5, static properties and dynamic properties

Static properties are stored in the stack, unlike dynamic properties stored in heap. Because all are in stack, and the instructions and data in the stack are fixed, it is easy to work out offsets, so both the class method (static and Non-static) can access the static properties of the class. Also because static properties are stored in the stack, they have global properties.

 6, summary

Java's heap is a run-time data area in which the object allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, the advantage of the heap is the dynamic allocation of memory size, the lifetime does not have to tell the compiler in advance, because it is dynamically allocating memory at runtime, the Java garbage collector will automatically take away these no longer used data. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. There are some basic types of variables in the stack (, int, short, long, byte, float, double, Boolean, char), and object handles.

Compared to the above parsing can be seen, in fact, Java processing heap and stack of the general principle and C + + is the same. There is just one more memory recycle mechanism that allows programmers to free memory without having to call delete voluntarily. Just as in C + +, the general use of new memory will be put into the heap, and the general temporary variables are placed inside the stack.

Today it's mainly about the differences in the stacks and stacks in Android's Dalvik, and what data to store. With Dalvik memory, Android also has a native memory concept.

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.