Java Virtual Machine JVM Summary (a) how the JVM works and the stack and heap implementations in the JVM

Source: Internet
Author: User

The Java-language source program is compiled into a platform-independent ' bytecode program ' (. class file, 0, 12 binaries) through the Java compiler, which is then interpreted in the Java interpreter above the OS, and the JVM is the core and foundation of Java. A virtual processor between the Java compiler and the OS platform.

First, the principle of the JVM

1. Introduction to the JVM:

The JVM is the core and foundation of Java, the virtual processor between the Java compiler and the OS platform. It is a software method to implement the abstract of the computer based on the underlying operating system and hardware platform, you can execute Java bytecode program.

The Java compiler generates code or bytecode files that the JVM can understand as long as it is oriented to the JVM. The Java source file is compiled into a bytecode program that translates each instruction into a different platform machine code through the JVM, running through a specific platform.


The process of 2.Java language operation

The Java-language source program is compiled by the Java compiler into a platform-independent ' bytecode program ' (. class file, which is the 0, 12-based program), and then interpreted in the Java interpreter above the OS.



The simple point is similar to the following principle


3. The JVM executes the procedure:

I. Load. class file

II. Manage and Allocate memory

III. Performing garbage collection

JRE (Java Runtime Environment) the running environment of Java programs constructed by the JVM




Ii. stacks and heap in the JVM

In the JVM, memory is divided into two parts, stack (stack) and heap (heap), where we know the stack and heap from the perspective of the memory management principle of the JVM, and these principles recognize the problem of static and static properties in Java.

1. Introduction

The stack (stack) is the memory instruction area of the JVM. Stack management is simple, push a certain length byte of data or instructions, stack pointer pressure stack corresponding byte displacement, pop a certain byte length data or instructions, stack pointer stack. Stack's speed is very

Fast, management is simple, and the data or instruction byte length of each operation is known. So Java basic data types, Java instruction code, constants are stored in the stack.

The heap is the memory data area of the JVM. Heap management is complex and allocates an indefinite amount of memory space each time, specifically to hold an instance of the object. Allocating a certain amount of memory in the heap to save an object instance is actually

Property values, the type of the property and the type tag of the object itself, and so on, do not save the object's method (the method is instruction, save in the stack), allocating a certain memory in the heap to save the object instance and the object's serialization comparison is similar. While the object instance is in the heap

, you need to save a 4-byte heap memory address in the stack to locate the object instance in the heap so that it is easy to find.


Architecture for the JVM


2, what is the data, what is the instruction, the method of the object and the properties of the object?

1) The method itself is the operation code part of the instruction, which is stored in the stack;

2) The internal variables of the method are part of the operand of the instruction, followed by the opcode of the instruction, saved in the stack (in fact, the simple type is saved in the stack, the object type holds the address in the stack, and the value is stored in the heap); The code and instruction operands constitute the complete Java directive.


3) The object instance includes its property values as data and is saved in the data area heap.

Non-static object properties are persisted as part of the object instance in the heap, and the object instance must be accessed through an address pointer saved in the stack. Therefore, the ability to access an object instance and its non-static property values depends entirely on the availability of an object instance's address pointer in the stack.


3. The difference between a non-static method and a static method:

There is a significant difference between a non-static method and a static method: a non-static method has an implicit incoming parameter, which is given to it by the JVM, regardless of how we write the code, and this implicit argument is the address pointer of the object instance in the stack. Therefore , a non- static method (the instruction code in the stack) can always find its own private data (object property values in the heap). Of course, a non-static method must also obtain the implied argument, so the non-static method must first new an object instance to obtain the address pointer in the stack before the call, otherwise the JVM will not be able to pass the implied parameter to the non-static method.


The static method does not have this implicit argument and therefore does not require a new object, which can be called as long as the class file is ClassLoader load into the JVM's stack. Of course, at this point the static method is not accessible to object properties in the heap.

Summary:when a class file is ClassLoader load into the JVM, the method instruction is saved in the stack, where the heap has no data. Then the program technology to start executing instructions, if it is a static method, the instruction code is executed directly, of course, the instructions are not allowed to access the heap data area, if the non-static method, because the implied parameter has no value, will be an error. Therefore, before a non-static method executes, the new object is assigned data in the heap, and the address pin in the stack is given to a non-static method, so that the program technology executes the instruction in turn, and the instruction code can now access the heap data area.


Static properties and Dynamic properties:

The previously mentioned object instances and the dynamic properties are all stored in the heap, and the heap must be accessed by the instruction (the method of the class) through the address pointers in the stack.

It is therefore possible to infer that static properties are stored in the stack and differ from the dynamic The state attribute is saved in the heap. Because all are in the stack, and the instructions and data in the stack are fixed-length, it is easy to calculate the offset, and therefore, regardless of the instruction (the method of the Class), you can access the static properties of the class. It is also because the static property is stored in the stack, so it has a global property.


In the JVM, static properties are stored in the stack instruction memory area, and dynamic properties are stored in the heap data memory area.


Summarize:

1), the stack is the unit of the runtime, and the heap is the unit of storage.

2), the problem of the operation of the stack solution, that is, how the program executes, or how to handle the data, the heap solves the problem of data storage, that is, how to put the data and where.


4, why should the heap and stack distinguish it?

First, from the point of view of software design, the stack represents the processing logic, and the heap represents the data. This separation makes the processing logic clearer. The idea of divide and conquer. This idea of isolation and modularity is reflected in every aspect of software design.

Second, the separation of the heap from the stack allows the contents of the heap to be shared by multiple stacks (and can also be understood as multiple threads accessing the same object). The benefits of this sharing are many. On the one hand, this kind of sharing provides an effective way of data interaction (for example: Shared memory), on the other hand, the shared constants and caches in the heap can be accessed by all stacks, saving space.

Third, the stack because of the needs of the runtime, such as saving the context of the operation of the system, the address segment needs to be divided. Because the stack can only grow upward, it restricts the ability to store content on the stack. While the heap is different, the objects in the heap can grow dynamically as needed, so the stack and heap splits make the dynamic growth possible, and only one address in the heap is required to be recorded in the corresponding stack.

The object-oriented is the perfect combination of heap and stack. In fact, the object-oriented approach to the implementation of the previous structured program is not any different. However, the introduction of object-oriented, so that the way to think about the problem has changed, and closer to the natural way of thinking. When we take the object apart, you will find that the object's properties are actually data, stored in the heap, and the object's behavior (method) is running logic, placed in the stack. When we write the object, we write the data structure, and also write the logic of the processing.


There is always a starting point for the program to run. Like the C language, main in Java is the starting point. Whatever the Java program, find main and find the entry for the program execution:)


5. What is stored in the heap? What is stored in the stack?

1), the object is stored in the heap. The stack is a reference to the base data type and the objects in the heap. the size of an object is not estimated, or can be dynamically changed, but in the stack, an object only corresponds to a 4btye reference.

2), Why not put the basic type in the heap? because the space it occupies is typically 1~8 bytes-it takes less space, and because it is a basic type, there is no dynamic growth-fixed length, so storage in the stack is enough, if there is no meaning to put him in the heap (it will be wasted space, explained later). It can be said that the basic type and object references are stored in the stack, and are a number of bytes, so when the program runs, they are handled in a uniform manner. But the basic type, the object reference, and the object itself are different, because one is the data in the stack, one is the data in the heap. One of the most common problems is the problem with parameter passing in Java.

3),the parameters in Java pass the simultaneous value? Or a reference? The program runs always on the stack, so when parameters are passed, there is only a problem passing the base type and object references. The object itself is not passed directly.

Java is called when a method call passes a parameter because there is no pointer.


PS: Stacks and stacks, the stack is the most fundamental thing to run the program. A program can run without a heap, but not without a stack. The heap is a data storage service for the stack, and the heap is a shared memory. However, it is the idea of the separation of heaps and stacks that makes Java garbage collection possible.


Java Virtual Machine JVM Summary (a) how the JVM works and the stack and heap implementations in the JVM

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.