Java Virtual Machine Knowledge Summary

Source: Internet
Author: User

Java Virtual machine in the process of executing a Java program, it manages the memory divided into a number of different data regions, some regions with the virtual machine process started, and some areas are dependent on the user thread start and end to build and destroy.    The Java virtual machine mainly divides the memory into: 1. Program counter: is a small amount of memory space, its role can be seen as the current thread execution of the byte code of the line number indicator.    The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction that needs to be executed, branching, looping, jumping, exception handling, and thread recovery need to rely on this counter to complete.    In order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, independent storage. If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, and if the native method is executed, the value of this counter is empty and the zone is the only one in the virtual machine specification that does not specify any memory overflow.    02. Virtual Machine Stack: It is also thread-private, the life cycle and the same thread, each execution of the method will create a stack frame, for storing local variable table, method return address and other information, each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the stack of the process.    The local variable table holds various basic data types, object references (possibly a reference pointer to the start address of the object, or a handle to the object) that can be known at compile time.    Data of type long and double takes up 2 local variable space and the remainder is one. A generic virtual machine stack can be dynamically scaled. There can be errors such as stack overflow and memory overflow in this area. 3. Local method Stack: relative to the virtual machine stack, the local method stack is serving the local method, and this area may have stack overflow and memory overflow in error.    4.java Heap: The largest piece of Java memory management, created when a virtual machine is started, whose sole purpose is to hold object instances where almost all object instances are allocated memory. All object instances and arrays are allocated on this region.    This is the main working area of the garbage collector. Java heap can be a physically discontinuous memory space, the main logical connection can be, it can be fixed size, can also be dynamically extended.    5. Method area: This is a thread-shared area of memory that holds the class information, constants, and static variables loaded by the virtual machine. This area also does not need to be physically contiguous. One of the most important areas is the running of the constant-rate pool. 6. Run a constant pool: part of the method area, which holds the various literal and symbolic references generated during compilation, which are stored in the run-time pool after the class is loaded. The Intern () method of the string is mostly used. Program counter, virtual machine stack, local method stack three regions are born with threads, and are extinguished with threads.

*************************************************************************************************************** Object access: Different virtual machines implement access to objects differently, and there are two main ways to access them: using handles and direct pointers. 1. If you use handle access, a chunk of memory is partitioned into the Java heap as a handle pool, where the object reference holds the handle address of the object, and the handle qualification contains the specific address information for the object and type data. 2. If a direct pointer is used, Java must consider how to prevent access to information about the type data in the layout of the object, which holds the address of the object in its reference. The advantage of using a handle is that the object reference holds a stable handle address that changes only the instance data pointer in the handle when the object is moved, and the reference itself is not modified. The advantage of using a direct address is that it is faster and saves the positioning overhead of a single pointer. How do I determine if an object is still alive? 1. Reference counting algorithm: To add a reference counter to the object, whenever there is a place to reference it, the counter plus 1, when the reference fails, the counter minus one, at any time the counter 0 of the object can no longer be referenced. This approach is simple and efficient, but the Java language does not use this algorithm to manage memory because it does not solve the problem of circular references between objects. Both 2.java and C # use a root search algorithm: The idea is to search through a series of objects called GC root as starting points, search all traversed paths as reference chains, and prove that this object is not available when an object goes to GC root without any reference chain. Objects that are unreachable in the root search algorithm are not dead and are not to be killed, and to actually reclaim an object, at least two times to mark the process. If the object does not discover a chain of references connected to the Gcroots after the root search, then he will be marked for the first time and filtered for the condition that it is necessary to finalize the () method when the object does not override the Finalize () method, or Finalize ( ) has been called by the virtual machine, and the virtual machine treats both cases as unnecessary, so that the object is immediately recycled s. If the object is bound to be necessary to execute the Finalize () method, then the object will be placed in a queue, and at a later time by a virtual machine automatically established, the lower priority of the thread to execute, where the execution refers to the virtual opportunity to trigger this method, but there is no guarantee that will wait for himRun end, the reason for this is that if the Finalize method of an object performs slowly, or if a dead loop occurs, it causes other objects in the queue to wait permanently, or even the entire memory recycling system crashes, and the Finalize () method is the last chance that the object will escape death. Later, the GC will mark the second small size of the objects in the queue, and if the object is to successfully save itself in the Finalize () method, simply re-associate with any object on the reference chain. For example, assign yourself (this) to a variable or a member variable of an object, and it will be removed from the queue when the second token is made. public class finalizeecapegc{public static FINALIZEEXCAPEGC save_hook = null; protected void Fianlize () throws throwable{super.finalize (); System.out.println ("Fianlize method executed!"); Finalizeecapegc.save_hook = this; Assign this to the member variable} public static void Main (String args[]) {Save_hook = new finalizeexcapegc (); Successful escape save_hook = null; System.GC (); Escape failure save_hook = null; System.GC (); }} in the code, the execution result is different because the Finalize () method of any object is called only once by the system, and if the object faces the next collection, its Finalize () method will not be executed again. So the second code of self-help action failed (that is, can only save themselves once). The operation of the Fianlize () method is expensive, uncertain, unable to guarantee the order of the individual objects, and the function of the Finalize () method can be replaced by try-finaily and used sparingly. Garbage collection in the method area mainly reclaims two parts of the content: Obsolete constants and useless classes. Take the string constant in the constant pool as an example, add a constant pool with a string constant "abc", but there is no string object reference"ABC" in a constant pool, memory recycling occurs at this time. The symbolic references to other classes (interfaces), methods, and fields in a constant pool are similar. The condition of judging whether a class is a useless class is much more harsh, The following three conditions are required: 1. All instances of this class have been reclaimed by 2. The ClassLoader that loaded the class have been reclaimed by 3. The corresponding Java.lang.Class object for this class is not referenced anywhere, and the method virtual machine that accesses the class in place by reflection can meet the above three conditions before the class is recycled , it is just "can".

*************************************************************************************************************** ******************
Common garbage Collection algorithms are: 1. Tag-purge algorithm 2. Copy algorithm 3. Tag-Collation algorithm 4. Generational-Collection Algorithm Java Virtual machine specification of how the garbage collector should be implemented without any provisions, different vendors, different versions of the virtual machine provided by the garbage collector can be a big difference In general, parameters are provided for users to assemble the collectors used in each era according to their own application characteristics and requirements. The automated memory management provided in the Java technology architecture can ultimately be attributed to the automated resolution of two problems: allocating memory to objects and reclaiming memory allocated to objects. When it comes to recycling memory, for allocating memory to objects, to the general direction, is allocated on the heap, the allocation of rules is not completely fixed, the details of which depends on which garbage collector is currently used in combination, and the virtual machine in the memory-related parameters of the settings. The implementation of language independence is still a virtual machine and bytecode storage format, is the Java compiler can be compiled Java code to store bytecode class file, while using some other languages can also compile program code into class file, virtual machine does not care about the source of class is what language, It can be run in a Java virtual machine as long as it conforms to the structure that his class file should have. The semantics of various variables, keywords, and operational symbols in the Java language are ultimately composed of multiple bytecode commands, so the ability of the bytecode command to provide a semantic description is certainly more powerful than the Java language itself. Language features that are not natively supported by some Java languages do not, therefore, imply that the bytecode itself cannot be effectively supported, which is the basis for the virtual machine to provide implementations for other languages.
*************************************************************************************************************** *****************
class file structure: The class file is a set of 8-byte-based binary streams, each data item is tightly packed in the class file in order, and no delimiters are added, which makes the entire class file store almost all the necessary data for the program to run.    No gaps exist.    When a data item that requires more than 8 bytes of space is encountered, it is segmented into several 8-bit bytes in front of the high-order storage.    The class file format is stored in a structure similar to that of the C language struct, with only two data types: unsigned number and table.    The unsigned number belongs to the basic data type, with U1, U2, U4, U8 representing the unsigned number of 1,2,4,8 bytes, respectively.    Unsigned numbers can be used to describe numbers, index references, quantity values, or string values by UTF-8 encoding.    A table is a conforming data type consisting of multiple unsigned numbers or other tables as data items, all of which are habitually terminated with "_info", and tables are used to describe the data of a hierarchical composite structure, and the entire class file is essentially a single table. Whether it is an unsigned number or a table, when it is necessary to describe the same type but a variable number of data, often using a pre-capacity counter plus a number of consecutive data items, this is called a series of continuous data of a certain type of a set. The first 4 bytes of each class file are called Magic Numbers, and its sole purpose is to determine whether the file is a class file that can be accepted by the virtual machine, and many file storage standards use magic numbers for identification purposes, using magic numbers instead of extension recognition primarily for security reasons. Because the extension can be changed very casually. The 4 bytes of the magic number are stored as the version number of the class file, the second and sixth bytes are the minor version, and the seventh and eighth bytes are the major version numbers.    followed by the major and minor version number is the constant pool entrance, the constant pool is the class file structure with the most associated with other items of data type, is the largest class file space occupies one of the data items, while also the class file in the first occurrence of the table type data item.    There are two main types of constants in a constant pool: literal and symbolic references.    Literal is like: a literal string, a constant value declared final. Symbolic references are concepts of compilation principles: include fully qualified names of classes and interfaces, field names and descriptors, method names, and descriptors. Java code in the Javac compile, not like C as a link to this step, but when the virtual machine load class file dynamic link, that is, the class file does not save the final memory layout information for each method and field, Therefore, the symbolic references of these fields and methods are not converted and cannot be directlyUsed by virtual machines, when the virtual machine is running, it needs to obtain the corresponding symbol reference from the constant pool, and then parse and translate it into the specific memory address when the class is created or run. After the end of the Chang, a 2-byte delegate access identity is followed, which identifies the access information at some class or interface level, including whether the class is a type or an interface, if it is defined as a public, or whether it is defined as an abstract type, or if it is a class. Whether it is declared final, and so on.
 ********************************************************************************************************** ***********************
The class loading mechanism of virtual machine;    The various information described in the class file will eventually need to be loaded into the virtual machine before it can be run and used. The virtual machine loads the data of the description class from the class file into the memory, verifies the data, transforms parsing and initializing, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine.    In the Java language, the loading and linking of types is done during the run of the program, which adds a little bit of performance overhead when the class is loaded, but it gives the application a high degree of flexibility.    Class from being loaded into the virtual machine's memory, to writing it out of memory, its entire lifecycle includes: load, validate, prepare, parse, initialize, use, and unload 7 phases, where validation, preparation, and parsing are collectively referred to as connections.    There is no mandatory constraint in the virtual machine specification. Under what circumstances it is necessary to start loading a class, but for the initialization phase, the virtual machine prescribes that there are only four cases where the class must be initialized immediately (while loading, validating, and preparing for the natural need to start before).    1. When encountering new, Getstatic, Putstatic, invokestatic Four bytecode instructions, if the class has not been initialized, the trigger must be initialized first.    2. When a class is invoked using reflection, the trigger initialization is required if the class has not been initialized.    3. When initializing a class, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized. 4. When the virtual machine starts, the user needs to specify a main class to execute (the class that contains the main method), and the virtual opportunity initializes the main class first. The detailed process of class loading: 1. Load, load phase the virtual machine needs to complete the following three things: get the binary byte stream that defines this class by the fully qualified name of a class to convert the static storage structure represented by this byte stream into a method the run-time data structures that represent this class are generated in the Java heap Java.lang . Class object as the access entry for this data in the method area. The load phase is the most controllable phase during development, because the load phase can be done using the system-provided classloader, or you can use a custom ClassLoader area to control how the byte stream is fetched. After the load phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the virtual machine, and the data storage format in the method area is defined by the virtual machine implementation itself. 2. Validation is the first step in the link phase, which is intended to ensure that the information contained in the byte stream of the class file conforms to the current virtual requirements and does not compromise the security of the virtual machine itself. However, the concrete implementation of the verification does not give a clear stipulation. The Javay language is a relatively safe language (vs. c), and the use of pure Java code does notsuch as accessing data outside the bounds of an array, transforming an object into a type that he did not implement, jumping to something like a nonexistent code.    Validation can be broadly divided into four phases: file format validation: Verifies that the byte stream conforms to the class file format specification and can be processed by the current version of the virtual machine.    Meta-data validation: Semantic analysis of the information described in bytecode to ensure that the information it describes conforms to the requirements of the Java language specification.    Bytecode validation: Data flow and control flow analysis ensures that the method of the class being validated does not act to compromise the security of the virtual machine at run time. Symbol Reference validation: Occurs when a virtual machine converts a symbolic reference to a direct reference, which occurs during the third phase of the link-the parsing phase. 3. The prep phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, which are allocated in the method area.    4. The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference.    Symbol Reference: A set of symbols to describe the referenced target, the symbol can make any form of the literal, as long as the use of a non-ambiguous positioning to the target can be. Direct reference: A direct reference can make a pointer directly to the target, a relative offset, or a handle that can be indirectly anchored to the target. 5. Initialization is the last step in the class loading process, which is the Java program code that really starts executing the definition in the class. For any class, it is necessary for the class loader to load it and the class itself to establish its uniqueness in the Java Virtual machine, that is, to compare whether two classes are "equal", only if the two classes are loaded by the same class loader, or even if the two are from the same class file, As long as they load their classloader differently, the two classes must be unequal. The term "equality" here includes the result of the Equals method of the class object and the return of the Isinstance method, as well as the case where the instanceof keyword is used to determine the relationship of the object. In the Java Virtual machine perspective, there are only two different classloader, one is to start the ClassLoader (implemented by C + +), which is part of the virtual machine, and all other ClassLoader (implemented by the Java language), and all inherit from Java.lang.ClassLoader. From the developer's point of view there are three types of system-supplied ClassLoader: 1. Launch ClassLoader: primarily responsible for loading class libraries in the <java_home>\lib directory and cannot be referenced directly by JAVA programs. 2. Extension class Loader: primarily responsible for loading class libraries in the <java_home>\lib\ext directory. 3. Application Class Loader: Responsible for loading the specified class library on classpath.

*************************************************************************************************************** ******************
The execution engine is one of the core components of the Java Virtual machine, and the execution engine of the virtual machine can make its own configuration of the instruction set and execution engine, and be able to perform any instruction set formats that are not directly supported by the hardware. In different implementations of virtual machines, the execution engine may have two options for interpreting execution (through the interpreter) and compiling, and possibly even several different levels of compiler execution engines, when executing Java code. In general, it can be seen as: the input is a bytecode file, the processing process is the equivalent process of bytecode parsing, output execution results. Now the mainstream virtual machine contains the instant compiler, the code in the class file will be interpreted or compiled execution, not necessarily. The Java Development Office has a compiler that can generate native code directly, and the C + + language has a version that is executed by the interpreter. The Java Virtual Machine Specification view defines a Java memory model that masks memory access differences between various hardware and operating systems to enable Java programs to bring consistent concurrency effects across various platforms. The main goal of the Java memory model is to define the access stocks for each variable in the program, that is, the underlying details of storing variables in the virtual machine to memory and removing variables from memory, where the variables refer to instance fields, static fields, and elements that make up the array object, but do not include local variables and method parameters. Because the latter is thread-private and will not be shared, there is no competition problem. The Java memory model stipulates that all variables are stored in main memory, that each thread has its own working memory, that the thread's working memory holds a copy of the main memory of the variable used by the thread, and that all operations on the variable must be made in working memory and not directly read and write to the variables in main memory. There is no direct access to variables in the other's working memory between different threads, and the transfer of variable values between threads needs to be done through main memory. When a variable is defined as volatile, it has two properties: Ensure that the variable is visible to all threads, that is, when a thread modifies the value of the variable, the new value is known to other threads, and the normal variable value does not. Because variable values are passed between threads, they need to be done through main memory.    However, this does not guarantee that the volatile variables will run safely under concurrency because the operations in Java are not atomic, and may be changed by other threads before the actual operation. Whether it is a normal variable or a volatile variable, the value of the variable flushed from the main memory before the variable is read is dependent on the main memory as the delivery medium for visibility, and the volatile variable eng ensures that the mind is immediately synchronized to the main memory and flushed from the main memory immediately before each use. The Java memory model requires that all eight operations of Lock,unlock,read,load,assign,use,store and write have atomicBut the read and write operations of the 64-bit data type (long,double) that are not vloatile modified are divided into two 32-bit operations.    In addition to volatile, the synchronized and final keywords are also visible: the visibility of the synchronization block is performed by synchronizing the secondary variable to the main memory before performing a unlock operation on a variable. Once the final keyword-decorated field is initialized in the constructor, and the constructor does not have a reference to "this," the value of the final field can be seen in other threads.

*************************************************************************************************************** ******************
Java Thread Implementation: Threads are more lightweight than the process of scheduling execution units, threading Introduction, can be a process of resource allocation and execution of scheduling separate, each thread can share process resources and can be scheduled independently. Each instance of the Java.lag.Thread class represents a thread, but the thread class differs significantly from most Java APIs, and all its key methods are declared as native, that is, the method may not be used or can not be implemented using platform-independent means. There are three main ways to implement threads: using kernel threading implementations, using user thread implementations, and loads lightweight process blending with user lines.    Thread scheduling refers to the process of assigning a processor to a thread, and there are two main scheduling methods: Collaborative thread Scheduling: The execution time of a thread is controlled by the thread itself, and after the thread has completed its work, it is necessary to proactively notify the system to switch to another thread. Preemptive thread scheduling: Each thread is assigned the execution time by the system, and the thread's switchover is not determined by the thread itself.    Thread scheduling in Java is done automatically by the system, but we can recommend that the system allocate a little more execution time to some threads, while others allocate one o'clock less--which requires the priority of the thread to be set.    When two threads are ready at the same time, higher priority threads are more likely to be executed by the system. The priority of the thread is not very reliable, because the Java thread is eventually mapped to the native thread of the system, and all thread scheduling is ultimately the operating system, and the priority of the actual operating system is not level 10.    State switching of Threads: the Java language defines 5 thread states: 1. NEW: The threads that have not been started after creation are in this state.    2. Ready: A thread in this state is waiting for the CPU to allocate execution time to him.    3. Run: The thread in this state is executing.        4. Wait indefinitely (wait): Threads in this state are not allocated CPU execution time, waiting to be Cheng by other lines.    Object.wait () and Thread.Join () will allow the thread to enter an indefinite wait state.    5. Wait (sleep): will not be allocated CPU execution time, but do not have to wait for the other line Cheng wake up, after a certain period of time they will be automatically awakened by the system.    6. Blocking: Waiting to get an exclusive lock. 7. End: The thread state of the thread has been terminated and the thread has finished executing. According to the "security degree" of thread safety, we can divide the data shared by various operations in the Java language into the following 5 classes: immutable, absolute thread-safe, relative thread-safe, thread-compatible, and thread-independent. 1. Immutable objects must be line ShuoThe full 2.vector is a thread-safe container, because its methods are synchronized modified, although inefficient, but indeed secure. 

Java Virtual Machine Knowledge 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.