Conscience production, right-click save for save

The Java virtual machine

The run-time data area

Thread private

  • Program counter

    • The only field that does not specify an OOM (OutOfMemoryError) is the address of the virtual machine bytecode instruction being executed (null if Native methods are being executed).
  • Java virtual machine stack

    • As each Java method executes, a stack frame is created to store information about local variables, operand stacks, dynamic links, method exits, and so on. The process from method invocation to completion corresponds to the process of loading and unloading frames in the Java virtual machine stack. (Local variables contain the base data type, object reference, and returnAddress types)
  • Local method stack

    • The Native method stack is similar to the Java virtual machine stack, except that the Native method stack serves Native methods.

Thread the public

  • Java Heap (GC section) (Java Head)

    • This is where almost all object instances are allocated memory and is the main area managed by the garbage collector. New generation and old age. For the new generation, it is divided into Eden space, From Survivor space and To Survivor space.
  • JDK1.7 Method area (permanent generation)

    • Used to store loaded class information, constants, static variables, just-in-time compiler compiled code, and so on. The main purpose of garbage collection in this area is to recycle the constant pool and unload the classes, but this is generally difficult to achieve. The HotSpot VIRTUAL machine treats it as a permanent generation for garbage collection. However, it is difficult to determine the size of the permanent generation because it is affected by many factors and the size of the permanent generation changes after each Full GC, so OOM exceptions are often thrown. Starting with JDK1.8, remove the permanent generation and move the method area into meta-space.

    • Run-time constant pool

      • The constant pool (literal and symbolic references generated by the compiler) in the Class file that is part of the method area is put into this area after the Class is loaded. Allows dynamic generation, such as the String intern()
  • JDK1.8 dimension

    • The data that used to be in the method area (the permanent generation) is moved partly to the Java heap and partly to local memory (the meta-space). The meta space stores the meta information of the class, static variables, constant pools, and so on into the heap.
  • Direct memory

    • In NIO, Native libraries are used to allocate out-of-heap memory directly.

Second, HotSpot VIRTUAL machine

Object creation

  • When the virtual machine reaches a new instruction
  1. Check if the parameter can find a symbolic reference in the constant pool, and check if the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, perform the corresponding classloading procedure first.
  2. After the class load check passes, the virtual machine next allocates memory for the new objects.
  3. After memory allocation is complete, the virtual machine needs to initialize all allocated memory space to zero (excluding object headers).
  4. Set the object headers as necessary.
  5. The execution constructor is initialized as the programmer wishes.

Object memory layout

    1. Object head
      1. The first part is used to store the runtime data of the object itself, such as hash code, GC generation age, lock status identification, thread held locks, bias thread ID, bias implementation stamps, etc.
      1. The second part is the type pointer, the pointer that an object points to its class metadata (if accessed using a direct object pointer), which the virtual machine uses to determine which class instance the object is.
      1. If the object is a Java array, the third part also records the data length.
    1. The instance data
    • Is the valid information that the object actually stores, that is, the content of the various types of fields defined in the code.
    1. Alignment filling
    • It’s not necessarily there, it’s just a placeholder. HotSpot requires that the object’s size be an integer multiple of 8 bytes.

Object access location

  • Handle access

    • Allocate a chunk of memory in the Java heap as a handle pool. An object reference on the Java stack stores the address of an object’s handle, which contains Pointers to object instance data and Pointers to object type data. The object instance data is in the Java heap and the object type data is in the method area (persistent generation). Advantages: Only the instance data pointer in the handle changes when the object is moved; the object reference itself does not need to be modified.
  • Direct pointer access (used by HotSpot)

    • The object reference on the Java stack stores the direct address of the object. The object instance data in the heap needs to contain Pointers to the object type data. Advantages: Saves the time cost of a pointer location and is faster.

Garbage collection

An overview of the

  • Garbage collection is primarily for the Java heap and method areas. Three areas of the program counter, Java virtual machine stack, and local method stack are thread private and disappear after a thread or method terminates, so there is no need to garbage collect these three areas.

Determines whether an object can be reclaimed

  • First Marking (Suspended)

    • Reference counting algorithm

      • Add a reference counter to an object. When a reference is added to the object, the reference counter is ++, when the reference counter is invalid –, and the object can be reclaimed if the reference counter is 0.

However, it is difficult to solve the problem of cyclic references between objects, where the two object references count to 1 and are never used.

- Reachability analysis algorithm (For Java) - Start with a series of GC Roots objects and search down from these nodes. The search path is called the reference chain. When an object is not connected to GC Roots by any reference chain, it is proved that the object is unavailable and can be reclaimed.Copy the code

GC Roots objects include

  1. Object referenced in the virtual machine stack (local variable table in the frame).
  2. An object referenced by a static attribute in a method area.
  3. The object referenced by the constant in the method area.
  4. Objects referenced by JNI (commonly referred to as Native methods) in the Native method stack.
  • Second mark

    • When the object does not overwrite the Finalize () method, or the Finalize () method has already been called by the virtual machine. If an object is re-associated with any object in the reference chain in the Finalize method, it will not be reclaimed.

    • finalize()

      • The Finalize () method of any object will only be called once by the system. It is a compromise, expensive to run, uncertain, and unable to guarantee the order of each object. Everything that can be done with Finalize () can be done better with a try-finally or some other way, and you can forget about it.

Method area recovery

  • Garbage collection in the method area is generally less cost-effective. There are two main parts of recycling: discarded constants and useless classes.

If the three criteria of useless classes are met, it only means that the collection can be carried out. It is not necessary and can be controlled by the -xnoclassGC parameter.

  1. All instances of the class have already been reclaimed, meaning that there are no instances of the class in the Java heap.
  2. The ClassLoader that loaded the class has been reclaimed.
  3. The java.lang.Class object corresponding to this Class is not referenced anywhere, and the methods of this Class cannot be accessed anywhere through reflection.

Reference types

    1. Strong reference
    • Create a strong reference using new a new object. As long as a strong reference exists, the referenced object is never reclaimed.
    1. Soft references
    • Use the SoftReference class to implement a SoftReference. A term used to describe objects that are useful but not required to be reclaimed before an out-of-memory exception occurs.
    1. A weak reference
    • WeakReference class is used to implement weak references. Weaker than soft references, the referenced object will be collected in the next garbage collection.
    1. Phantom reference
    • Use the PhantomReference class to implement virtual references. The weakest reference relationship has no effect on the lifetime of the referenced object and cannot obtain an object instance through a virtual reference. The only purpose is to receive a system notification when the object is reclaimed by the collector.

Garbage collection algorithm

    1. Mark-clear
    • First, mark all objects that need to be reclaimed. After the completion of the marking, all the marked objects are reclaimed and unmarked.

Inadequate:

  1. Efficiency issues, both marking and cleaning processes are inefficient.
  2. Space issues, a large number of discrete memory fragments are generated after the tag is cleared, and there is no contiguous memory to hold larger objects and another garbage collection has to be triggered early.
    1. Mark-tidy
    • It is the same as the mark-clear algorithm, but after the mark, all living objects are moved to a segment, and then the memory beyond the end boundary is cleaned up directly. The space problem of mark-clear algorithm is solved, but a large number of objects need to be moved, and there are still efficiency problems.
    1. copy
    • Divide the available memory into two equally sized pieces by capacity and use only one piece at a time. When this area of memory is used up, surviving objects are copied to the other area, and the excess space is cleaned up again. The cost is to reduce the memory to half of the original, too high.

Commercial virtual machines are now using this algorithm for the next generation. Since 98% of objects in the new generation are born and die, the memory is divided into a large Eden space and two smaller Survivor Spaces, and Eden and one Survivor space are used each time. When recycling occurs, if there is not enough space in another Survivor space to hold the surviving objects, the objects will go directly to the old age through the allocation guarantee mechanism.

    1. Generational collection
    • Java heap is generally divided into new generation and old generation. Replication algorithm is used in the new generation, and mark-clean or mark-tidy algorithm is used in the old generation for recycling.

HotSpot algorithm implementation

  • Enumeration root (GC Roots)

    • Today’s mainstream Java virtual machines use exact GC. During GC pauses, the virtual machine knows what type of data is at what offsets within the object through the OopMap data structure (map table), and specific locations record which locations in the stack and registers are references. Therefore, GC Roots enumeration can be completed quickly and accurately.
  • safer

    • To save GC space costs, the OopMap is not generated for every instruction, but is only recorded at “specific locations” called safe points.

Program execution can only be suspended when a safe point is reached, and there are two ways to reach the safe point.

  1. Grab break (rarely used). During GC, all threads are interrupted, and if any thread is not at the safe point, it is restored and allowed to run to the safe point.
  2. Active interrupt (main use). During GC, a flag is set that each thread polls when it reaches a safe point and suspends the thread if the flag is found to be straight.

However, when a thread is sleep or blocked, unable to respond to the JVM’s interrupt request, it walks to a safe point to interrupt and suspend, leading to the safe zone.

  • The safety area

    • A security zone is an extended safe point in a code fragment where reference relationships do not change.

When a thread enters a safe zone, it enters a safe zone, and the JVM does not need to take care of that thread when GC occurs. When the thread leaves the safe zone, check whether the system has completed the GC process, and wait for the signal that can leave the safe zone, otherwise continue to execute.

Garbage collector

  • The new generation

      1. Serial collector
      • It is a single-threaded collector that uses only one thread for garbage collection and, more importantly, must suspend all other worker threads while garbage collection takes place.

Advantages: Simple and efficient compared to other single-threaded collectors, with no overhead of thread interaction for a single CPU environment, thus having the highest single-threaded collection efficiency.

It is the default new generation collector for Client scenarios, where memory is generally not very large.

- 2. Parnew collector - This is a multi-threaded version of the Serial collector that uses a considerable amount of common code.Copy the code

There is absolutely no better performance than the Serial collector in a single CPU environment, or even 100 percent better performance in two CPU environments.

It is the default Cenozoic collector in the Server scenario, mainly because it can only be used in conjunction with the CMS collector in addition to the Serial collector.

- 3. Parallel Scavenge collector - the ParNew collector.Copy the code

Whereas the goal of other collectors is to minimize the amount of time that user threads pause during garbage collection, its goal is to achieve a manageable throughput. Throughput here refers to the amount of time the CPU spends running user programs as a percentage of total time.

  • The old s

      1. Serial old collector
      • Is an older version of the Serial collector.

It is also used for VMS in the Client scenario.

- 5. Parallel Old Collector - Is an older version of the Parallel Scavenge collector.Copy the code

The Parallel Avenge and Parallel Old collector are preferred in applications where throughput is important and CPU resource sensitive.

The Concurrent Mark Sweep collector is a collector whose goal is to obtain the shortest collection pause time. - Operation process - 1. Initial mark (minimum). You still need to suspend the user thread. It's just a quick way to mark objects that GC Roots can be directly associated withCopy the code
  1. Concurrent flags (longest time). The process of GC Roots Tracing.
  2. Relabel. Corrects the mark record of the part of the object whose mark changed during concurrent marking because the user program continued to operate. Longer than the initial tag but much less than the concurrent tag time.
  3. Concurrent remove

Steps 1 and 4 do not have the word concurrency, that is, they still suspend the user thread.

- Pros and cons - Concurrent collection, low pauses.Copy the code
  1. The CMS collector is very sensitive to CPU resources. While it does not cause user threads to stall, it does slow down the application by taking up CPU resources.
  2. Unable to handle floating garbage. In concurrent sweep phase new rubbish also will continue to produce, so when the GC to control “- XX: CMSinitiatingOccupancyFraction parameters” set aside enough memory space for the rubbish, A “Concurrent Mode Failure” occurs when the reserved memory fails to meet the requirements of the program, temporarily starting Serial Old collection.
  3. Due to the mark-sweep algorithm, a large amount of space debris is generated after collection.
    1. G1 collector
    • Garbage First is a Garbage collector for service-oriented applications

    • Operation process

        1. Initial tag
  1. Concurrent tags
  2. In the end tag
  3. Delete selected tag

Class loading mechanism

An overview of the

  • The virtual machine loads the data describing the Class from the Class into memory, verifies, transforms, and initializes the data, and finally forms Java types that the virtual machine can use directly. The high degree of flexibility in Java applications is achieved by relying on runtime dynamic loading and dynamic wiring.

Class life cycle

  • Load -> Connect (Verify -> Prepare -> Parse) -> Initialize -> Use -> uninstall

Class initialization timing

  • Active reference

    • The virtual machine specification does not enforce when to load, but it does specify that there are five and only five cases in which classes must be initialized (loading, validation, and preparation all occur)
  1. New, getstatic, putstatic, and Invokestatic are not initialized when encountered.
  2. Reflection calls without initialization.
  3. If the parent class is not initialized, the initialization of its parent class is triggered first.
  4. The class that contains PSVM (mian () method).
  5. REF_getStatic, REF_putStatic, REF_invokeStatic method handles when dynamic languages support them.
  • Passive reference

    • Except for the five cases above, none of the ways to refer to a class trigger initialization, called passive references.
  1. Referring to a static field of a parent class by a subclass does not result in subclass initialization.
  2. Referring to a class through an array definition does not trigger initialization of the class. This procedure initializes the array class, which is a virtual machine generated subclass that directly inherits Object and contains the array properties and methods. Users can only use the public length and Clone () values.
  3. Constants are stored in the constant pool of the calling class at compile time and are not directly referenced to the class that defines them, so they do not trigger initialization of the class that defines them.

Class loading process

    1. loading
      1. Gets the binary byte stream that defines the class by its fully qualified name.
  1. Transform the static storage structure represented by this byte stream into the runtime data structure of the method area.
  2. An in-memory java.lang.Class object representing the Class (which HotSpot stores in the method area) is generated as an access point to the various data of the Class in the method area.
    1. validation
    • To ensure that the information contained in the byte classes of the Class file meets the requirements of the current VM and does not compromise vm security. Most class validation can be turned off by -xverify: None.
  1. File format verification. Ensure that the input byte stream is properly parsed and stored in the method area. The next three validations are all based on the method area storage structure and do not manipulate the byte stream.
  2. Metadata validation. Semantic analysis of bytecode description information to ensure that it conforms to Java syntax specifications. (Java syntax validation)
  3. Bytecode verification. The most complex, through data flow and control flow analysis, determine the semantics of the program legal, logical. It can be turned off with a parameter. (Verify instruction jump range, valid type conversion, etc.)
  4. Symbol reference validation. The conversion of symbolic references to direct references occurs in the third phase, the parsing phase.
    1. To prepare
    • Class variables are static variables. The preparation phase allocates memory for class variables and sets a zero value (final directly sets the initial value), using memory in the method area.
    1. parsing
    • The process of replacing symbolic references in a constant pool with direct references. In some cases, the parsing process can begin after the initialization phase to support Java’s dynamic binding. Parsing actions are for classes or interfaces, fields, class methods, interface methods, method types, method handles, and call point qualifiers.
    1. Initialize the
    • The initialization phase actually executes the Java program code defined in the class and is the process of executing the class constructor () methods. In the preparation phase, class variables are given zero values, while in the initialization phase, class variables and other resources are initialized according to a subjective plan made by the programmer through the program.

      • (a)

        • Class constructor method. Is generated by the compiler automatically collecting the assignment action of all class variables in a class and combining statements in a static statement block.
  1. There is no need to explicitly call the parent constructor, and the JVM guarantees that the parent clinit execution is complete before the subclass Clinit executes.

  2. An interface cannot use static statement blocks but can still have assignment operations for class variables. Clinit of the child interface does not need to execute the parent interface’s Clinit method first when variables defined in the parent interface are not used. The implementation class of the interface also does not execute the clinit methods of the interface.

  3. The virtual machine ensures that Clinit is properly locked and synchronized in a multi-threaded environment. Other linear awakenings do not enter clinit methods, and a type is initialized only once under the same classloader.

    - <init>() - Object constructor method. Java objects are instantiated only when they are created, initialized for non-static variable resolution.Copy the code
  4. The init method of the parent class is explicitly called, and all initialization of the instance field during object instantiation takes place in the init method.

Class loader

  • Classes and class loaders

    • The class loader implements the loading action of a class. The class loader and the class itself establish the uniqueness of the class. Each class loader has a separate class namespace. Two classes are equal only if the same classloader is loaded. Equality includes the equals() method of the Class object, isAssignableFrom(), isInstance(), and instanceof keyword.
  • Class loader classification

    • Start the class loader

      • Implemented by C++ language, is a part of the virtual machine. Is responsible for the JAVA_HOME/lib directory, or the path specified by the -xbootclasspath parameter, but the name must be recognized by the virtual machine, the name does not match can not be loaded by the startup class loader. The startup class loader cannot be referenced directly by a Java program.
    • Extend the class loader

      • Implemented by the Java language and responsible for loading the JAVA_HOME/lib/ext directory, or any libraries in the path specified by the java.ext.dirs system variable, developers can use the extended class loader directly.
    • Application class loader

      • Since this ClassLoader is the return value of the getSystemClassLoader() method in ClassLoader, it is also commonly referred to as the system ClassLoader. It loads the libraries specified on the user’s ClassPath, which is usually the default class loader in the program.
    • Custom class loaders

      • Implemented by the user.
  1. If you don’t want to break the parent delegate model, you can simply override the findClass method.
  2. Otherwise, the entire loadClass method is overridden.
  • Parental delegation model

    • The parent delegate model requires that all class loaders have their own parent class loaders, except for the top-level start class loaders. Instead of being implemented as an inherited relational class, a parent and child use a combinative relationship to feed the parent loader’s code. Implemented in the loadClass() method of java.lang.classLoader.

    • The working process of the

      • A classloader first forwards classloading requests to the parent classloader and only tries to load it itself if the parent cannot complete (it does not find the required class in its search scope)
    • benefits

      • Java classes, along with their classloaders, have a hierarchy of priorities that allow the base class library to be agreed upon.

4. Memory allocation and reclamation strategy

Minor and Full GC

  • Minor GC

    • Garbage collections occur in the new generation, and because the new generation objects are short-lived, Minor GC is performed frequently and quickly.

    • The timing

      • Eden is insufficient
  • Full GC

    • Full GC is usually accompanied by Minor GC, which is more than 10 times slower than Minor GC.

    • The timing

        1. Call System. The gc ()
        • It is recommended that the virtual machine perform Full GC, but the virtual machine does not necessarily perform Full GC. This approach is not recommended, but rather lets the virtual machine manage memory.
        1. There is not enough space in the old era
        • A common scenario is for large objects and long-lived objects to age. Try to avoid creating objects and arrays that are too large, and increase the size of the new generation so that objects can be recycled as much as possible in the new generation without going into the old age.
        1. Method area space is insufficient before JDK1.7
        • When there are too many classes to load, reflected classes, and constants in the system, the permanent generation may be filled up. Full GC will be performed even if CMS GC is not configured, and an OOM exception will be raised if there is not enough space. You can increase the method area space or switch to CMS GC.
        1. Space allocation guarantee failed
        • Two judgment failures to allocate guarantees when Minor GC occurs
        1. Concurrent Mode Failure
        • During the CMS GC Concurrent cleanup phase, the user threads are still executing, new floating garbage is constantly generated, and when the reserved space is insufficient, a Concurrent Mode Failure is reported and Full GC is triggered.

Memory allocation policy

    1. Objects are allocated in Eden first
    • In most cases, objects are allocated on Eden of the new generation. When Eden space is insufficient, Minor GC is initiated. When another Survivor space is insufficient, the surviving objects are transferred to the old age in advance through the allocation guarantee mechanism.
    1. Big object goes straight to the old age
    • Configuration parameters – XX: PretenureSizeThreshold, greater than the worth objects directly in the old s allocation, to avoid a large amount of memory replication between Eden and Survivor.
    1. The long-lived object enters the old age
    • The virtual machine defines an Age counter for each object. When Eden is born and the object is migrated to another Survivor space through Minor GC, Age++ will be migrated to the old Age if the object increases to 16 by default.
    1. Dynamic object age binding
    • The virtual machine does not always require the age of objects to reach MaxTenuringThreshold to be promoted to the old age. If the total size of all objects of the same age in Survivor is greater than half of the Survivor space, the objects older than or equal to this age directly enter the old age.
    1. Space allocation guarantee
    • Before a Minor GC occurs, the virtual machine checks to see if the largest contiguous space available in the old generation is larger than all objects in the new generation, and if so, the Minor GC is considered safe. The HandlePromotionFailure parameter can be set to allow the risk that the virtual machine will perform a Full GC if it is still smaller than the average size of objects promoted to the aged zone over the ages. After JDK1.6.24 HandlePromotionFailure has no effect, that is, the virtual machine defaults to true.