Java Fundamentals (vi) Java JVM memory

Source: Internet
Author: User
Tags modifiers

Java Virtual machineJava Virtual machine is a Java file generated by the class file, the class file is the bytecode, the JVM can explain the bytecode becomes the machine code of the target machine, so the Java program can be compiled once and then run around the real implementation of the cross-platform features, The JVM is key in cross-platform features. including our own built-in class files require a virtual machine to interpret execution. Of course, implementing this feature is not a simple matter, and the JVM has done a lot of things for us in this process, and it has its own set of standard design structures to suit different program health. knowing how a program works in a virtual machine can design better, better-performing programs that are important to understand how the virtual machine works and the structure of each department. Memory structureJVM Structure diagram The following is a JVM structure that is described from different angles, and what are the structural components that can be viewed in comparison:Logical Structure:

from the above logical view can not only visually see the JVM, JDK, JRE three kinds of relationship between the three, you can see the difference between the differences in common and different, JDK more than the JRE Java syntax support and some Java tool classes to provide, The JRE is just a running environment at the bottom.
Physical Structure:

 in the physical structure, you can see the change of a class file from entering the JVM to the completion of the execution process.

in the diagram we can adjust the department or the part that our developers can manage is the JVM memory allocation this blockWhat data is stored in each zone

java heap storage : When a Java class instantiates an object or an array, the JVM allocates memory space to it in the heap, and only one heap of all threads in the JVM share a heap area.

method Area : All threads share a method area, and the size of the method area can be specified by the program runtime, such as initialization size, minimum value, maximum value,

Java Stack store:

1, type information (type information)
For each type that is loaded, the following information is stored in the Java virtual machine in the method area:
1), the full name of the type (the fully qualified name of the type)
2), the full name of the type's parent type (unless there is no parent type, or the Frey form Java.lang.Object) (the fully qualified name of the Typeís direct superclass)
3), to type is a class or interface (class or an interface) (Whether or not, the type is a class)
4), type modifier (public,private,protected,static,final,volatile,transient, etc.) (the Typeís modifiers)
5), List of all parent interface names (an ordered list of the fully qualified names of any direct superinterfaces)
The data structure saved by the type's full name is defined by the virtual machine's implementation. In addition to this, the Java Virtual machine also holds the following information for each type:
1), type Chang (the constant pool for the type)
2), type fields information (field information)
3), type methods information (method information)
4), All static class variables (non-variables) information (all class (static) declared in the type, except constants)
5), a reference to the ClassLoader (a reference to class ClassLoader)
6), a reference to the class (a reference to class class)

1), type Chang (the constant pool for the type)
A constant pool holds an ordered set of constants for all types, including direct constants (literals), constants such as strings, integers, floating-point numbers, and symbolic references to types, fields, and methods. Every persisted constant in a constant pool has an index, just like a field in an array. Because a constant pool holds a character reference to the type, field, and method used by all types, it is also the primary object of the dynamic connection. For more information, see Chapter Sixth, "The Java Class File".
2), type fields information (field information)
Field names, field types, field modifiers (Public,private,protected,static,final,volatile,transient, and so on), the order in which fields are defined in the class.
3), type methods information (method information)
The method name, the return value type of the method (or void), the number of method arguments, the type and their order, the field's modifier (public,private,protected,static,final,volatile,transient, and so on), The order in which the method is defined in the class
If not abstract and local this law still needs to be saved
The byte code of the method, the size of the operand stack of the method, and the size of the local variable area (details later), exception list (see Chapter 17th "Exceptions" for more information). )
4), class (Static) variable (class Variables)
Class variables are shared by instances of all classes, even if they are not accessed through instances of the class. These variables are bound on the class (not on the instance of the class), so they are part of the logical data for the class. You need to allocate memory for class variables (non-final) Before the Java Virtual machine uses this class
The constant (final) is handled differently than this class variable (non-final). Each type is copied to its own constant pool when a constant is used. Constants are also stored in the method area like class variables, except that they are stored in a constant pool. (It is possible that class variables are shared by all instances, whereas constant pools are unique to each instance). The Non-final class variable is saved as part of the definition of his type data (that is, declares them), and the final constant is saved as an Part. See chapter Sixth "The Java class Filethe Java class File" for details
5), reference to the ClassLoader (A reference to class ClassLoader)
Each type that is loaded by a Java virtual machine, the virtual machine must save whether the type is loaded by the original class loader or the ClassLoader. Those types that are loaded by the ClassLoader must hold a reference to the ClassLoader. This information is used when the ClassLoader is dynamically connected. When a class references another class, the virtual machine must save that the referenced type is loaded by the same ClassLoader, which is also the process of maintaining a different namespace for the virtual machine. See chapter Eighth "The linking Model" for details
6), reference to class class (A reference to class Class)
The Java Virtual machine creates an instance of the Java.lang.Class class for each loaded type. You can also use the class method:
public static Class forname (String className) to find or load a class and obtain an instance of the corresponding class class. With this class instance, we can access the information in the Java Virtual Machine method area. Refer to the Javadoc of class in detail.
2. Methods list (method Tables)
For more efficient access to all data stored in the method area, the storage structure of the data must be carefully designed. In all method areas, in addition to the original information above, there is a data structure designed to speed up access, such as a list of methods. For each non-abstract class that is loaded, the Java Virtual machine generates a list of methods that hold references to all instance methods that the class might invoke, giving an error to the methods called in the parent class. See chapter Eighth "The linking Model" for details


Local method Stack:

The local method stack is actually the same as the Java stack, but the service object is not the same, the Java stack for the Java Virtual machine Execution Java method service and the local method stack for the virtual machine to perform the native method service, there is no obvious difference between the two areas, and some virtual machines are merged, such as Sun The hotspot writes them together collectively as a stack.


The heap can then be subdivided into the following areas:



In order to make reasonable use of memory space, it can improve the efficiency of code execution.

Summarize:

This is just a macro view specific to how the code dynamically changes in memory, but also requires a 1.1-point analysis of the process, each step contains a lot of content needs us to delve into the virtual machine inside, it will be more understanding of its operating mechanism.


Java Fundamentals (vi) Java JVM memory

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.