Java Virtual Machine Understanding Discovery 1

Source: Internet
Author: User

The following is a summary of the individual's understanding of the deep Java virtual machine

Basic concepts:

A Java Virtual machine can refer to an abstract specification, or to a specific implementation, or to a Java virtual machine instance.

Virtual Machine life cycle:

The bounden duty of a Java Virtual machine instance is to be responsible for running a Java program. When a Java program is started, a virtual machine instance is born. The program shuts down and the virtual machine instance is also extinct. If you run three Java programs on the same computer, you will get three instances of the Java virtual machine. Each Java program runs with its own instance of the Java virtual machine.

A Java Virtual machine instance runs a Java program by calling the main () method.

The main task of the garbage collector is to automatically reclaim the memory of objects referenced by programs that are no longer running. In addition, it is possible to move those objects that are still in use to reduce fragmentation.

Before talking about garbage collection, first understand the internal architecture of the Java Virtual machine, as shown in

The middle dashed box part is the run Time data region, which consists of 5 parts, namely the method area, heap, stack, program counter and local method stack.

1. Program counter

For a running Java program, each of these threads has its own PC register, which is created when the thread is started.

Its role can be seen as the position indicator of the current bytecode execution.

2. Local Method Stack

Any local method interface will use the local method stack. When the thread calls the local method, the virtual opportunity creates a new stack frame and presses the Java stack, but when it calls the local method, the virtual opportunity keeps the Java stack intact and no longer presses the new frame in the thread's Java stack. A virtual machine is simply a dynamic connection and invokes the specified local method directly.

3. Method area

In a Java virtual machine, information about the loaded type is stored in memory that is logically referred to as the method area. class variables (static variables) are also stored in the method area. The method area is thread-shared and must be designed to be thread-safe.

4.java Stack

When a new thread is started, the Java Virtual machine assigns it a Java stack, and the Java stack saves the thread's running state in frames. The virtual machine only performs two operations directly on the Java stack: the stack and the stack in frames.

The method that a thread is executing is called the current method of the thread, the stack frame used by the current method is called the current frame, the class to which the current method belongs is called the current class, and the current class's Chang is called the current constant pool.

When a thread executes a method, it keeps track of the current class and the current constant pool.

All data on the Java stack is thread-private, and no thread can access the stack data of another thread, so there is no need to take into account the access synchronization of data in multi-threaded stacks.

5.java Heap

All class instances and arrays created by the Java program at run time are placed in the same heap. Each Java program is exclusive to one instance of the virtual machine----they do not interfere with each other, and there is only one heap space in a Java virtual machine instance, and all threads share the heap, in which case the synchronization of the heap data must be considered in the case of multithreading.

Java-Class life cycle

A Java mount, connect, initialize see:

The class is initialized and can be used, the program can access its static field, call its static method, or create its instance, the object.

The class is instantiated in 4 ways: 1. Explicit new 2.Class, or the Newinstance () method of the Construtor object, 3. Invokes the Clone () method of any existing object 4.objectinputstream.getobject () method to deserialize.

Once an object is no longer referenced, it needs to be garbage collected. As for How,when, the garbage collection depends on the virtual machines. The garbage collection mechanism for common hotspot virtual machines is described below.

Java Virtual Machine Understanding Discovery 1

Related Article

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.