Deep understanding of Java Virtual Machines-Learning Note 1

Source: Internet
Author: User

Java Memory Area program counter (Programs Counter Register)

is a small amount of memory space that can be seen as the line number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, the bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, exception handling, thread recovery, and so on, need to rely on this counter to complete.

At any given moment, a processor (a kernel for a multicore processor) will only execute instructions in one thread. Therefore, each thread needs to have a separate program counter. This type of memory is "thread-private" memory.


If the thread is executing a Java method, this counter logs the geology of the virtual machine bytecode instruction being executed, and if the native method is being executed, this counter is empty. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerro conditions.


Bytecode (byte code)

is a binary file that contains the execution program. For Java, a class file is composed of bytecode, which is not directly run by the JVM, and is interpreted and executed (converting bytecode into machine code).

Java bytecode execution takes the following 3 steps:

(1) The class loader is responsible for loading the class file (. class file) into the Java virtual machine. In this procedure, you need to verify that the class file conforms to the class file specification.


(2) The Bytecode Checker (bytecode verifier) checks the code of the class file for certain illegal operations, such as writing the local computer file system in an applet program.


(3) If the bytecode checker is passed, the Java interpreter is responsible for interpreting the class file as machine code for execution.


Java Virtual machine in the "sandbox" run mode, that is, the Java program code and data are limited to a certain amount of memory space to execute, do not allow the program to access memory outside the memory space. In the case of applets, access to the file system of the client machine is not allowed.

Java VM Stack (Java Virtual machine Stacks)

As with the program counter, it is also thread-private, with the same life cycle as the thread. The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic linked lists, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.


Usually someone divides the Java memory area roughly into heap memory and stack memory, the stack refers to the virtual machine stack, or the local variable table part of the virtual machine stack. The local variable table holds various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types, different from the object itself) that are known at compile time. It may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other location associated with the object, and the ReturnAddress type, which points to the address of a bytecode directive.


64-bit lengths of long and double data consume 2 local variable spaces (slots), and the rest of the data types occupy only one. If the thread requests a stack depth greater than the virtual machine allows, the Stackoverflowerror exception is thrown, and if the virtual machine stack can be dynamically extended (most of the Java Virtual machines can) and cannot request enough memory, the OutOfMemoryError exception is thrown.

Local methods Stack (Native method Stacks)

The role of virtual machine stacks is very similar, but the difference is that virtual machine warfare executes Java methods (i.e. bytecode) services for virtual machines, while the local method stack is the native method service used by virtual machines. The local method stack will also throw out Stackoverflowerror and OutOfMemoryError exceptions.

Java heap (Java heaps)

is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this memory area is to hold object instances where almost all of the object instances are allocated memory. The description in the Java Virtual Machine specification is that all object instances and arrays are allocated on the heap. However, with the development of the JIT compiler and the gradual maturation of the escape analysis technology, the stack allocation, scalar substitution optimization technology will cause some subtle changes to occur, all objects are allocated to the heap is gradually become less absolute.


The Java heap is the main area of garbage collector management, so it is often referred to as the "GC Heap" (garbage collected heap). From the memory recovery point of view, because the collector now uses the Generational collection algorithm, so Java heap can also be subdivided into the new generation and the old age, and then a little more detailed Eden space, from Survivor space, to survivor space. From a memory allocation perspective, a thread-shared Java heap may divide multiple thread-private allocation buffers (thread Local Allocation buffer, Tllab). Regardless of the area, the object instance is stored. A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

Method area

Like the Java heap, it is an area of memory shared by each thread that stores data such as class information, constants, static variables, even compiler-compiled code, that have been loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has a name called Non-heap, which should be distinguished from the Java heap. Memory reclamation in this area is primarily for the collection of constant pools and for unloading types.


Many people are accustomed to the method area as a "permanent generation" (Permanent Generation), which is essentially not equivalent, simply because the design team of the hotspot virtual machine chooses to extend the GC collection to the method area, or to use the permanent generation to implement the method area. This allows the hotspot garbage collector to manage this part of the memory just like the Java heap, eliminating the effort to write memory management code specifically for the method area. There is no permanent generation concept for other virtual machines. Using a permanent generation to implement a method area is not a good idea because it is more prone to memory overflow problems and very few methods (such as String.intern ()) can cause different performance under different virtual machines for this reason. Java8 has eliminated the concept of a permanent generation and the use of meta-space (metaspace), the size of the meta-space based on local memory (Native memories) and the entire system of available memory, greatly alleviating the problem of OutOfMemoryError. One idea behind Metaspace is that the life cycle of a class and its metadata is consistent with the life cycle of its classloader. In other words, as long as the class loader is alive, the class metadata in Metaspace is also alive and cannot be freed.

Run a constant pool (runtime Constant)

is part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool, for the compilation period generated by a variety of literal and symbolic references, which will be loaded in the class load in the run-time pool of the method area to store. A OutOfMemoryError exception is thrown when a constant pool is no longer able to request memory.

Direct Memory

is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the Java VM specification. But this part of the memory is also used frequently, may also lead to the appearance of OutOfMemoryError. For example, NIO introduces a channel-and buffer-based I/O approach, which can be used to directly allocate out-of-heap memory using the native library and then operate through a Directbytebuffer object stored in the Java heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and the native heap.

In-depth understanding of Java virtual Machines-Learning Note 1

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.