Android Program Optimization-----Java class life cycle

Source: Internet
Author: User

Objective:

What does a virtual machine, class do in memory? This is the prerequisite knowledge of program Optimization! Want to write a program optimization of the series of articles, mainly written in the popular to let a person understand, of course, the rigor of the reduction . After all, I'm not likely to develop virtual machines! If you want to study or read the "deep understanding of Java Virtual Machine" This book of God. Spit three liters of blood to recommend this book, the book to the. class files, virtual institutions, how to implement, how to optimize the talk of the incisively.


Virtual institutions:

Run-Time data regions (several areas of the Java virtual machine that are partitioned in memory):

You think about it! We wrote. The. java file is compiled to form a. class file. The logic in the class name A, the method name, the constant constant, and the method in Java is all mapped to the. class file, and the. class file is loaded into the virtual machine memory, and the information is certainly stored in memory. So where is this information stored in memory? In fact, this process is a layer of screening, the human easy to understand the redundant information---"to the machine can understand the extremely concise information 0101.

Example:

public class A{private int A = 3;public static final int CONSTANT = 32;}

public class b{static{system.out.println (a.constant);}}

1. Heaps (heap)

Account for the largest piece of memory. Storing information

A) the field value of the class object, such as the value of the Class A object a property 3. (The object is different and the field value is different, so constant is not stored here, because all Class A objects have only one value of 32)

b) The address in the method area where the class-invariant information is stored (the information that does not change in Class A objects is: Field name A, class name a, modifier private.) You have n objects, and the field names of the classes don't change.

2. Method area

Storage information:

A) class invariant information: Field name A, class name a, modifier private

b) The runtime Constant pool is used to store symbolic references and literal constants generated during compilation. (for example, the. class file bytecode is represented by the constant attribute value 3)

-----------------------------above are thread-public-------------------------

3.JAVA Virtual Machine Stack

Stack frame (stack Heap), store local variable table, Operation Stack, dynamic link, method exit and other information

4. Local Method Stack

Similar to a virtual machine stack, just describes a local method with a modifier of native.

5. Direct Memory

6. Program counter

Log the number of lines that the program has made to the byte code.



To optimize, you have to know how the class works in memory.

Class life cycle:

Load (Loading)- Validate (verification)-Prepare (preparation)---(Resolution)--"Initialize (initiation)---" Use (using)---- "Uninstall (unloading). Where the verification of yellow---"Preparation---" Resolution is called the connection (linking).

(Android comes with a proguard feature called Preverification,android which is blocked.) In Java Micro Edition and Java 6 the validation is separated and can be pre-verified during compilation (preverification))

Where the loading and joining phases do not execute the code we write (unless you rewrite the ClassLoader), the initialization phase begins.

Load:

1) Get the binary file of this class by the fully qualified name of the class (package name + class name)

2) class static storage structure (methods, fields, modifiers depicted in. class files) converted to method area run-time data structures

3) Generate a Java.lang.Class object representing the modified class in the Java heap as the access entry for the method area


Java constant static plus final, the constant is replaced with 32 directly during compilation. In other words, a class is not loaded in the run of B to read the constant variable, but instead refers to the constant pool constant of B itself. This saves memory because the Class A is not loaded.


In Java bytecode: with final

Android Program Optimization-----Java class life cycle

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.