JVM implementation and operation principle Stack and the Heap

Personal blog navigation page (click on the right link to open a personal blog): Daniel take you on technology stack 

Java language source code written in the Java compiler, compiled into a platform-independent 'bytecode program' (.class file, which is 0,1 binaries), then the Java interpreter on the OS interpreted, the JVM is the foundation and core java, java virtual processors between the compiler and os platform.

A, JVM principle

1. JVM Introduction

JVM is the foundation and core java, java virtual processors between the compiler and os platform. It is the bytecode program utilizing software-implemented method of computer-based abstraction of the underlying operating system and hardware platform can be performed in the above java.

java compiler for as long as the JVM, builds JVM code or byte code files can be appreciated. Java source files compiled into bytecode program, each instruction by the JVM will translate into machine code on different platforms, run by a particular platform.

[/ erji] 2. Process Java language to run [/ erji]
Java language source program written in the Java compiler, compiled into a platform-independent 'bytecode program' (.class file, which is 0,1 binaries ), then interpreted in the Java interpreter on the OS.

JVM implementation and operation principle Stack and the Heap

3. JVM process of execution of the program

I. loading class files.

II. Management and memory allocation.

III. Performs garbage collection.

JRE (java runtime environment) by the structure of java program JVM runtime environment

JVM implementation and operation principle Stack and the Heap

Two, JVM in the Stack and Heap

In the JVM, memory is divided into two parts, Stack (stack), and Heap (heap), here we come to know Stack and Heap memory management from the perspective of the principle of the JVM, and a clear understanding of Java static methods and static by these principles property issues.

1 Introduction

Stack (Stack) is JVM instruction memory area. Stack management is very simple, push a length of bytes of data or instructions, push Stack pointer corresponding byte displacement; POP certain length in bytes of data or instructions, Stack pointer is popped. Stack of fast, easy management, and each data byte length instruction or operation are known. Therefore, basic data types Java, Java instruction code, the constants are stored in the Stack.

Heap (heap) the JVM data memory area. Heap management is very complex, variable length of each allocation of memory space, designed to save the instance of the object. Heap allocation memory to store a certain object instances, in fact, only the object instance stored attribute value, the attribute type and the type of the object itself and other marks, the method does not save the object (the instruction is stored in the Stack) , allocating a memory object stored in the object instance and the Heap comparison of similar sequences. After the object instance and a good distribution in the Heap, you need to be saved in the Stack Heap a 4-byte memory address used to locate the position of the object instance in the Heap, easy to locate the object instance.

The following figure shows the JVM architecture:

JVM implementation and operation principle Stack and the Heap

2. What is the data, what is the instruction, what methods and properties of an object is an object?

1) method itself is the opcode portion of the instruction stored in the Stack;

2) The method of internal variables as the instruction operand portion, following the opcode of the instruction stored in the Stack (simple type actually stored in the Stack, Stack object type stored in the address, the value stored in the Heap); said instruction opcodes and instruction operand constitute a complete Java instructions.

3) including the object instance attribute value as the data stored in the data area the Heap.

Non-static object properties as part of the object instance is stored in the Heap, and the object instance must be saved by the Stack Pointer address to access to. Thus you can access to the object instance and its non-static property values ​​entirely depends on the availability Stack address pointer in the object instance.

3. The difference between the non-static methods and static methods?

Non-static methods and static methods have a very significant difference: non-static methods have an implicit incoming parameter, which is the JVM to it, and how we write independent code, this implicit parameter is the object instance in the Stack the address pointer. Thus non-static method (Stack instructions in the code) can always find their private data (object attribute value of the Heap). Of course, non-static method must also obtain the hidden parameters, non-static method before calling must be a new object instance, get the address pointer Stack, otherwise JVM will not be hidden parameters passed to non-static method.

Static method is no such hidden parameters, and therefore do not need new objects, as long as the class file is ClassLoader load into the Stack JVM, which static method can be called. Of course, at this time it is a static object attribute access is not the Heap.

Summary: When a class file into the JVM ClassLoader load, the method instructions retained in the Stack, at this time no data is Heap. TECHNICAL then begins executing instructions, if the method is static, direct sequence executed instruction code, the instruction code is of course at this time can not access the data area Heap; if non-static method, since there is no implicit parameter values ​​being given. Therefore, before performing a non-static method, first new objects in the Heap allocation data, and the non-static method to the Stack address pointer so that the program is sequentially executed instructions technology, the instruction code will now be able to access the data Heap zone.

Static and dynamic attributes:

The aforementioned dynamic object instance and attributes are stored in the Heap, and Heap Stack address pointer must be able to be in the (class methods) to access instructions.

Thus it can be inferred: Static attributes are stored in the Stack, unlike the dynamic attributes are stored in the Heap. Because in all the Stack, and the Stack instructions and data are fixed length, so it is easy to calculate the offset, therefore no matter what instructions (class methods), can access a static class property. It is because of the static properties are stored in the Stack, it has a global property.

In the JVM, the static attributes are stored in the instruction memory area Stack, Heap dynamic attributes are stored in the data memory area.

to sum up:

1) is a unit of run-time stack, and heap storage units.

2) Stack program to solve operational problems, namely how to program execution, or how to handle the data; heap solve data storage problems, namely how to put the data, where to put.

4. Why should distinguish between heap and stack out of it?

First, from the point of view of software design, stack represents processing logic, and data representing the heap. This separation, so that the processing logic clearer. The idea of ​​divide and rule. This isolation, modular thinking is reflected in all aspects of software design.

Second, the isolated stack and the stack, the stack contents so that a plurality of stacks may be shared (to be understood as a plurality of threads to access the same object). This sharing benefits are many. On the one hand this provides an efficient sharing of data interaction (such as: shared memory), on the other hand, heap cache can be shared and constant access to all of the stack, saving space.

Third, because of the need runtime stack, such as saving the context of the system is running, we need to divide the segment. Since the stack can only grow up, so it will be limits on the ability to stack storage content. Heaped different objects in the heap can be increased dynamically as needed, stack and heap split, so that it becomes possible to grow dynamically, a record only the corresponding address in the stack to stack.

Fourth, is the perfect combination of object-oriented heap and stack. In fact, object-oriented programming with the way the previous structure of the program does not make any difference in the implementation. However, the introduction of object-oriented, so that thinking approach to the problem has changed, but more natural way of thinking is close. When we open the object, you will find that the object's properties is actually the data stored on the heap; and the behavior of the object (method), is to run the logic on the stack. When we write object, in fact that is the preparation of the data structure, logic also written to process data.

To run the program there is always a starting point. As with the C language, java is the starting point in Main. Whatever java program, find the main entrance to find a program to be executed :)

5. What is the heap memory? What the stack deposit?

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

2) Why not put basic types heap it? Because of the space it occupied is generally from 1 to 8 bytes - less space is needed, and because the basic type, so it will not appear in the dynamic growth - a fixed length, so the stack is stored enough, if he there is little sense of the heap (also a waste of space, described later). It can be said, citing the basic types and objects are stored in the stack, and a few are a number of bytes, so the program runs, their approach is unified. But the basic types, object references, and the object itself there is a distinction, because a data stack is a heap of data. The most common problem is that problems in Java parameter passing.

3) When the Java parameter passing by value in it? Or reference? When the program is running is always carried out in the stack, thus passing the parameters, delivery problems exist only basic types and object references. It does not directly pass the object itself.

When the Java method call passes parameters, because there is no pointer, it is performed for a call-

PS: heap and stack, the stack is running the most fundamental thing. Program can not run the heap, but not without stack. The heap is a data storage service for the stack, heap that white is a piece of shared memory. However, precisely because of ideological separation of the heap and stack, which makes it possible to Java garbage collection.

Attached Java / C / C ++ / machine learning / Algorithms and Data Structures / front-end / Android / Python / programmer reading / single books books Daquan:

(Click on the right to open there in the dry personal blog): Technical dry Flowering
===== >> ① [Java Daniel take you on the road to advanced] << ====
===== >> ② [+ acm algorithm data structure Daniel take you on the road to advanced] << ===
===== >> ③ [database Daniel take you on the road to advanced] << == ===
===== >> ④ [Daniel Web front-end to take you on the road to advanced] << ====
===== >> ⑤ [machine learning python and Daniel take you entry to the Advanced Road] << ====
===== >> ⑥ [architect Daniel take you on the road to advanced] << =====
===== >> ⑦ [C ++ Daniel advanced to take you on the road] << ====
===== >> ⑧ [ios Daniel take you on the road to advanced] << ====
=====> > ⑨ [Web security Daniel take you on the road to advanced] << =====
===== >> ⑩ [Linux operating system and Daniel take you on the road to advanced] << = ====

There is no unearned fruits, hope you young friends, friends want to learn techniques, overcoming all obstacles in the way of the road determined to tie into technology, understand the book, and then knock on the code, understand the principle, and go practice, will It will bring you life, your job, your future a dream.

Published 30 original articles · won praise 0 · Views 904

Guess you like

Origin blog.csdn.net/jishulaozhuanjia/article/details/104716778