JVM Learning Summary

Source: Internet
Author: User

What is a JVM

The JVM contains three layers, abstract specifications, using the specification to define a virtual computer, the virtual computer has its own instruction set, a specific implementation, according to the abstract specification, for different hardware platforms, has implemented a virtual machine, an instance of a running virtual machine, after the implementation of the virtual machine, To use a virtual machine, you first create an instance of the virtual machine, and then load the instance of that virtual machine into memory, a Java application that corresponds to an instance of a virtual machine, and a virtual machine instance that serves only one Java application.
The life cycle of an instance of the JVM, as the application starts and ends with the end of the application, the instance of the virtual machine runs on the resource of the system-created process, running in the kernel-level thread that the system creates for the process, and the application runs in the user-level thread created by the virtual machine. Threads in the JVM are divided between user-level threads and daemons, with only one initial user-level thread, and the main thread for the main method in the application, which is typically used by the JVM, such as garbage-collected threads, where the daemon thread is characterized by the end of all user-level threads. The JVM can mark a user-level thread as a daemon thread, regardless of whether the daemon's statement finishes executing.

Memory allocation for the JVM

Java memory allocation refers to the memory allocation of the JVM, also known as the JVM Runtime data area, first in the physical memory to partition an area, and then in the region to load a running instance of only one JVM, the JVM instance will be from the region to simulate the PC register, stack, heap, method area, data segment, code snippet.
The PC register is a unique memory for each user-level thread of the JVM instance, and is used to record the address of the next instruction that the thread is executing, fast.
The stack is unique to each user-level thread of the JVM instance, and its members consist of a stack frame that holds the execution state of the Java method that the thread is executing, including the local basic data type variable, the local reference type variable, the parameter of the method, the return value, the intermediate result of the operation, For Java local methods that are called by JNI, the stack frame is temporarily considered to hold only a reference to the local method stack, fast.
The value of a reference type variable is a Long value, the stack of the JVM is growing downward, that is, to the low address extension, the JVM has no data registers, the intermediate results of instruction set operations are stored in the stack frame as a stack of threads, and the stack frame contains the execution state of a method executed by the thread, each of which executes a method. is to create a stack frame for the method should be pushed into the stack, after the execution of the method, the stack frame pop-up stack, the local method's stack frame only a reference to the local method stack, the execution state of the local method is saved in the local method stack, the local method stack has actually jumped out of the JVM, and the specific platform, Multithreading does not have synchronization problems when accessing the respective stack members, and the stack uses the enjoy meta-mode to hold the stack members.
For example, int a = 3,int b = 3,a = 4, execute the first statement, query the thread's stack memory for 3, do not open a stack space and deposit 3, while a point to the stack space, execute the second statement, query the stack space for 3, there is a direct B point to the stack space, execute the third statement, Query stack space is 4, not a stack space and deposit 4, while a point to the stack space, the change of a value is not directly modify the contents of the stack space pointed to a, but again point to another stack space.
The heap is owned by the JVM's kernel-level thread, and can be shared by all user-level threads of the JVM's instance, with multiple threads having synchronization problems with access to the heap members, with no fast stack, and a solid zone and constant pool.
The entity area holds the entities that reference type variables point to, whether it is a variable of a local reference type or a variable of a member reference type, and they point to entities that are stored in the entity area of the heap, including the entity of the array, and the entity area only holds variables of the object entity's member base data type and member reference type variables. Does not include the entity and member methods that the member reference type variable points to, and personally believes that there should be a reference to the method area method.
The constant pool holds the class, interface, method identifier declarations, local constants, member constants, and the use of the enjoy meta mode.
The method area is the kernel-level thread owned by the JVM, and can be shared by all user-level threads of the JVM's instance, storing only the member methods of the entity, without storing the data, at a faster speed than the stack.
The data segment belongs to the kernel-level thread owned by the JVM, can be shared by all user-level threads of the JVM's instance, has synchronization problems, holds the static properties of the class, and is faster than the stack.
The code snippet belongs to the kernel-level thread owned by the JVM, and can be shared by all user-level threads of the JVM's instance, with the code snippet storing only the source code and not storing the data at a faster speed than the stack.

Constant pool of the JVM

The basic data type of Java encapsulates the class Byte,short,integer,long,float,double,character,boolean, except for the Float and Double, with the constant pool used for data between the remaining types 128-127.

//s1 and S2 point to the same space, referencing the same. StringS1="How is"StringS2="How is"//s3,s4 points to different entity areas, which are referenced differently. StringS3= New String("How is You")StringS4= New String("How is You")////Two object entities created in memory, first the JVM checks to see if the constant pool has "ABC", No Open space//between the deposit ABC, then in the physical area to open space storage ABC. StringS5= New String("ABC")Three object entities created in memory, both in a constant poolStringS6="ABC"+"D"Seven object entities are created in memory, all in a constant pool, and additional objects are generated for//a,b,c,d,ab,abc,abcd,+, if StringBuilder//The Append does not produce additional objects. StringS7=A+"B"+C+"D" PublicClass Test {//Member basic data type variable i value 3 stored in entity area     Publicint I= 3;The value of the member reference type variable A is stored in the entity area, and a pointed entity opens its own real    //Body Area storage     PublicA A= NewA ();The value of the//member reference type variable ITG1 is stored in the entity area, 3 in the constant pool     Public IntegerItg1= 3;The value of the member reference variable ITG2 is stored in the entity area, and the entity that Itg2 points to opens its own    //Physical area Storage 3     Public IntegerItg2= New Integer(3);The value of the//member reference type variable ITG3 is stored in the entity area, because 128 is in the -128-127    //Outside, the entity that ITG3 points to uses automatic type encapsulation to open up its own physical area for storage    //128     Public IntegerItg3=  -; Public voidTest () {//local basic data type variable i value 3 stored in the stackint I= 3;The value of a local reference type variable A is stored in the stack, a pointing entity opens up        //own physical area storageA A= NewA ();//Local reference type variable s1 value stored inside the stack, string constant how        //are you in the constant pool        StringS1= " How is";//local reference type variable ITG1 value is stored in the stack, 3 uses a constant pool        IntegerItg1= 3; }}
Garbage collection of the JVM

The garbage collector of the JVM is dynamically reclaiming heap memory space occupied by object entities without any object references at intervals, such as when the JVM is idle, by System.GC () or Runtime.getruntime (). GC () to notify the JVM to do a garbage collection, However, notification is notified, when the JVM is not necessarily, we have no control, by the JVM itself, the JVM has seven kinds of garbage collector.
   Reference count Collector, each object entity in the Entity area maintains a reference counter that records the number of references to that object entity, and when the value of a reference counter for an object entity is 0, the object entity can be garbage collected, and the value of the reference counter for any object entity referenced inside the object is correspondingly reduced by one. The collection of one of the objects in this method may cause other objects to be recycled.
The advantage is that the reference counter is embedded in the program and run with the program, can judge the value of the reference counter in real time, fast collection, suitable for the real-time environment.
The downside is that the increase or decrease in the reference count brings additional overhead and cannot handle circular references, such as two objects, a, a, a, B, entity A, and Entity B referencing entity A in object A, entity a with object A, and entity A as a reference count of 2. The reference count for a reference and B in a ' refers to 2, which is a reference to B reference with B ' in A, and a = null, and entity A is not collected because B in a ' refers to a, unless B is collected, and B = null, which does not cause B to be collected, Because B ' references in A are not empty, and entities A and B are not controlled by the programmer at this time.
   Trace Collector, Mark and clear two stages, the marker stage will traverse from the root object of the reference tree, mark each object entity encountered, and the purge phase will release the object entity that is not marked.
   Compression Collector, which handles the heap fragments produced in the trace collector, slides the object entities that are not collected in the heap to one end of the heap, leaving the other end to a large contiguous idle area, the object entity is moved, and the object entity's reference is updated simultaneously, using an intermediate handle table to really point the records in the table to the entities in the heap. References to records in a table, when moved by an object entity, simply modify the handle table without modifying the object's references, which may be implemented in Java without using the address directly, but using the reference.
   Copy Collector, in the process of traversing the reference tree, all the object entities that are traversed are copied to a new region at the same time and stored continuously in the new zone, so that the free fragments of the original area, and the object entities that are not traversed, are directly considered to be idle areas. As a result of the process of iterating through the copy, the whole process of marking and clearing is completed, and the heap fragments can be cleared, and the heap is divided into two regions, which can use only one region at any time, and when the space of one area is exhausted, the allocation is stopped, and the copy operation is called Stop and copy.
The downside is that you need twice times the size of the physical area, because only half of the solid area can be used at a time.
According to the collector, the entity area is divided into sub-entity area, each sub-entity area is a generation, there is an age layer, the youngest generation of sub-entity area of the object is collected the highest frequency, which is stored in most of the life cycle is very short, they are run out of the probability of large, naturally collected probability of large, After several collections of objects in the child entity area, objects that are not collected are considered to be long-life objects that are transferred to the older generation of child entities, where the life cycle of the objects in the child entity area is mostly longer, and the number of garbage collections that are performed is reduced.
   Adaptive Collectors, which is adaptive to adjust the JVM's garbage collector according to the various garbage collector scenarios described above, so that the JVM can use a variety of garbage collectors instead of just one garbage collector.
   Progressive garbage collector, you can avoid collecting all of the garbage at a time to lead to too long, affect system performance, and even be aware of the user, only a partial collection of garbage at a time, to ensure that each garbage collection is limited to a certain timeframe, usually by the use of the garbage collector to achieve.

JVM Learning Summary

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.