What is a JVM?

Source: Internet
Author: User

Description: Java development Almost all know the name of the JVM, but because the JVM to the actual simple development of the association is not much, generally work for a year or two (of course, do not include love learning and specifically to do performance optimization of what), very few people can be very good to learn and understand what is the JVM, As well as figuring out how the JVM works, I think this piece is a very important thing to learn and learn, especially in Java development, which is the cornerstone of Java, just getting started or getting started shortly.

JVM (Java virtual Machine,java vm)

The cross-platform feature of Java programs is that bytecode files can be run on any computer or electronic device that has a Java virtual machine, and the Java interpreter in the Java Virtual Machine is responsible for interpreting the bytecode file as a specific machine code to run. Therefore, at run time, the Java source program needs to be compiled into a. class file by the compiler. It is well known that Java.exe is the Java class file execution program, But in fact the Java.exe program is just an execution of the shell, it will load Jvm.dll (Windows, under the Windows platform for example, under Linux and Solaris is actually similar to: libjvm.so), this dynamic connection library is the actual operation of the Java Virtual machine processing.

The JVM is part of the JRE. It is a fictitious computer, it is realized by simulating various computer functions on the actual computer. JVM has its own perfect hardware architecture, such as processor, stack, register, etc., also has the corresponding instruction system. The most important feature of the Java language is running across platforms. The JVM is used to support operating system-independent, cross-platform implementations. So, the Java virtual machine JVM belongs to the JRE, and now we install the JDK with the JRE installed (and, of course, the JRE separately).

JVM Memory Area Division

Roughly, the internal architecture of the JVM is divided into three parts: the class loader (ClassLoader) subsystem, the runtime data area, and the execution engine.

Class Loader

Each Java Virtual machine consists of a class loader subsystem (class loader subsystem) that is responsible for loading the types (classes and interfaces) in the program and giving unique names. Each Java Virtual machine has an execution engine (execution engine) responsible for executing the instructions contained in the loaded class. The two kinds of loaders of the JVM include: Start class loaders and user-defined class loaders, the Startup class loader is part of the JVM implementation, the user-defined class loader is part of the Java program, and must be a subclass of the ClassLoader class.

Execution Engine: It either executes the bytecode, or executes the local method

The main implementation techniques are: Interpretation, immediate compilation, adaptive optimization, chip-level direct execution which is interpreted as belonging to the first generation of the JVM, the JIT is the second generation of the JVM, adaptive optimization (currently used by Sun's HOTSPOTJVM) to draw on the experience of the first generation JVM and the second generation JVM, Adopt the combination of the two ways.

Adaptive optimizations: Start by explaining how all of the code is executed, monitor code execution, and then start a background thread for frequently called methods, compile it into local code, and carefully refine it. If the method is no longer used frequently, the compiled code is canceled, and it is still interpreted for execution.

Runtime data area: Main include: Method area, Heap, Java stack, PC register, local method stack

    • Method areas and heaps are shared by all threads

Heap: Holds objects created by all programs at run time

Method Area: When the JVM's class loader loads the. class file and parses the type information into the method area.

    • Java stacks and PC registers are exclusive to the thread

The JVM stack is thread-private, and each thread creates a JVM stack that is stored in the JVM stack as a variable of the local base type in the current thread (eight basic types defined in Java: Boolean, Char, Byte, short, int, long, float, Double), partial return result, and stack Frame, objects of non-basic type are placed on the JVM stack only one point to the heap address

    • Local method Stack: Stores the state of the local method call
JVM run-time data area

Because the data area of the JVM runtime is of particular importance to us in terms of development, let's go.

    • Method area

In the Sun JDK, this area corresponds to Permanetgeneration, also known as the persistent generation.

The method area holds information about the class being loaded (name, modifier, and so on), static variables in the class, constants defined as final in the class, field information in the class, method information in the class, and when the developer passes through the GetName in the class object in the program, Isinterface and other methods to obtain information, these data are derived from the method area, and the method area is also shared globally, under certain conditions it will also be GC, when the method region needs to use more memory than its allowable size, will throw outofmemory error message.

    • Heaps (heap)

It is the area where the JVM stores object instances and array values, and it can be assumed that all the memory of objects created by new in Java is allocated here, and that the memory of the objects in the heap needs to wait for the GC to recycle.

The heap is shared by all threads in the JVM, so the allocation of the object memory on it requires locking, which also results in a larger cost for the new object

In order to increase the efficiency of object memory allocation, the Sun Hotspot JVM allocates a separate space Tlab (thread Local Allocation Buffer) for the created thread, whose size is calculated by the JVM based on the running situation. There is no need to lock the object on Tlab, so the JVM allocates memory to the thread's objects as much as possible on the Tlab, in which case the performance of allocating object memory in the JVM is as efficient as C, but if the object is too large it is still directly using heap space allocation

Tlab only works on the new generation of Eden Space, so when writing Java programs, it is often more efficient to allocate smaller objects than large objects.

    • Javastack (Java Stack): virtual machines only perform two operations directly on Javastack: stack or stack in frames

Each frame represents a method, and the Java method has two return methods, return and throw exceptions, both of which cause the frame of the method to stack up and free memory.

Frame composition: The local variable area (including method parameters and local variables, for the instance method, also first save the This type, where the method parameters are placed strictly in the order of declaration, local variables can be placed arbitrarily), the operand stack, frame data area (to help support the resolution of the constant pool, Normal method return and exception handling).

    • Programcounter (program counter)

Each thread has its own PC register, which is also created when the thread starts. The contents of the PC register always point to the next hungry address where the instruction will be executed, where the address can be either a local pointer or an offset relative to the method's start instruction in the method area.

If thread executes the Java method, the PC holds the address of the next execution instruction. If thread executes the native method, the value of the PC is undefined

    • Nativemethodstack (local method Stack): Save the address of the native method into the zone

Depending on the implementation of the local method, such as the local method that a JVM implements to use the C connection model, the local method stack is the C stack, which can be said that when a thread calls the local method, it enters a realm that is not constrained by the JVM, which means that the JVM can dynamically extend itself using local methods.

JVM Garbage Collection

Sun's jvmgenerationalcollecting (garbage collection) principle is that it divides objects into younger generations (young), older generations (tenured), and persistent generations (Perm), using different algorithms for objects of different lifecycles. (Based on the object life cycle analysis)

usually we say that the JVM memory recycle is always referred to as heap memory recycling, indeed only the content in the heap is dynamically requested to allocate, so the young and old generations of the above objects refer to the JVM's heap space, and the persistent generation is the previously mentioned Methodarea, not the heap.

The rationale of GC is to recycle objects that are no longer in use in the GC, which is called collectors for recycling, and because the GC consumes some resources and time, Java, after analyzing the life-cycle characteristics of the object, collects the objects in the new generation, the old generation, To minimize the pauses the GC makes to your application

(1) The collection of new generation objects is called minor GC;

(2) The collection of objects of the old generation is called full GC;

(3) The GC for the active invocation of System.GC () in the program is the full GC.

Different object reference types, the GC is recycled in different ways, and the reference to the JVM object is divided into four types:

(1) Strong references: By default, objects are strongly referenced (instances of this object have no other object references, and GC is recycled)

(2) Soft reference: Soft reference is an application available in Java that is appropriate for caching scenarios (GC only if memory is not enough)

(3) Weak reference: GC is bound to be recycled

(4) Virtual reference: Because the virtual reference is only used to know whether the object is GC

    • Young (younger generation)

The young generation is divided into three districts. One Eden area, two survivor districts. Most objects are generated in the Eden area. When the Eden Zone is full, the surviving objects will be copied to the Survivor area (one of two), and when the survivor area is full, the surviving objects of this area will be copied to another survivor area, when the survivor is full, Objects that are copied from the first survivor area and that are still alive will be duplicated in the old age zone (tenured. It should be noted that the two areas of the survivor are symmetrical and have no relationship, so the same area may exist at the same time from Eden copied objects, and from the previous survivor copied objects, and copied to the old quarter only from the first survivor to come over the object. Moreover, there is always an empty survivor area.

    • Tenured (old generation)

Older generations store objects that survive from younger generations. In general, older generations are storing long-term objects.

    • Perm (persistent generation)

Used to store static files, now Java classes, methods, and so on. The persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, at which point a large, persistent generation space is required to store the new class in these runs. The persistent generation size is set by-xx:maxpermsize=.

What is a 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.