Java runtime data area:

During the execution of Java programs, the Java VIRTUAL machine divides the managed memory into several data areas. These areas have their own purposes and creation and destruction time. Some areas exist with the start and end of the vm process, and some areas are created and destroyed depending on the start and end of the user thread. The memory managed by the Java virtual machine includes the following runtime data areas, as shown:

1. Program counter: refers to the bytecode instruction being executed by the current thread. Thread private. 2. Virtual machine stack: The virtual machine stack is the memory model for Java execution methods. When each method is executed, a stack frame is created, pushed onto the stack, and when the method returns normally or throws an uncaught exception, the stack frame is removed. (1) Stack frame: information related to stack frame storage method, including local variable table, return value, operand stack, dynamic link A, and local variable table: including all variables in the process of method execution. The space needed for the local array of variables is allocated at compile time, and the size of the local array does not change during the run of the method. Return value: pushes the return value, if any, into the operand stack in the caller’s stack frame and points the VALUE of PC to an instruction address following the method call instruction. C. Operand stack: memory model for manipulating variables. The maximum depth of the operand stack is determined at compile time (written to the max_stacks TAB of the method area code property). The elements of the operand stack can be any Java type, including long and double, and the 32-bit space on the stack is 1 and the 64-bit space is 2. When the method is first executed, the stack is empty, and as the method executes, various bytecode instructions fetch and unload data onto the stack. D. Dynamic linking: Each stack frame holds a reference to the method it belongs to in the run-time constant pool, which is held to support dynamic linking during method calls. 3. Local method stack: (1) call native memory model (2) thread exclusivity. 4. Method area: used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by virtual machines (1) Constant pool shared by threads (2) Runtime constant pool:

A deposit, is part of the method B, compile time generated A variety of literal and symbolic reference C, in addition to being A Class file version of A Class, field, method, interface description information, and there’s A constant pool, exists A compile time of this Class is generated A variety of literal and symbolic references, this part will be in after Class loading, To the runtime constant pool in the method area.

5, Heap (Heap) : Java object storage place (1) the Java Heap is one of the biggest in the management of the virtual machine memory block (2) the Java Heap is all threads sharing area (3) in the virtual machine starts to create (4) the area of memory’s sole purpose is to store an object instance, almost all object instances allocate memory here. (5) The Java heap is the area of memory managed by the garbage collector and is often referred to as the “GC heap”

JMM Java memory model:

Java concurrency uses the “shared memory” model, where threads communicate by reading and writing the common state of memory. Multiple threads cannot interact with each other by passing data directly; they can only interact by sharing variables. 2. The main purpose is to define the access rules for each variable in the program. 3. The Java memory model specifies that all variables are stored in main memory and that each thread has its own working memory. (1) The working memory of the thread stores a copy of the variables used by the thread (copied from the main memory). All operations on variables by the thread must be performed in the working memory instead of directly accessing variables in the main memory. (2) Different threads cannot directly access the variables of each other’s working memory, and the transfer of variable values between threads must be completed through the main memory. (3) Main memory mainly corresponds to the instance data part of the Java heap. Working memory corresponds to a portion of the virtual machine stack.

4. Communication between Java threads is controlled by the Java Memory Model (JMM). (1) The JMM determines when a variable written by one thread is visible to another thread. (2) Shared variables between threads are stored in main memory (3) each thread has a private local memory that stores a copy of the read/write shared variables. (4) THE JMM provides programmers with memory visibility assurance by controlling the interaction between the local memory of each thread. (1) When a shared variable has copies in multiple local memory, if one local memory modifies the copy of the variable, other variables should be able to see the modified value. This is visibility. (2) Ensure the orderly execution of threads, which is orderliness. (To ensure thread safety) 6, intermemory operation: (1) lock: the main memory variable, a variable is identified as a thread exclusive state. (2) UNLOCK: a variable that acts on the main memory. It releases a locked variable so that it can be locked by other threads. (3) Read: reads a variable from main memory into working memory. (4) Load: applied to working memory to load variables read into working memory into a copy of variables in working memory. (5) Use: applied to working memory to pass values of variables in working memory to an execution engine. Assign: a variable that applies to the working memory. Assigns values received by the execution engine to variables in working memory. (8) Write (write) : write the value of the store operation into the variable of the main memory. (1) One of the read, load, store, and write operations is not allowed to occur separately. (2) One thread is not allowed to discard assgin operations. (3) One thread is not allowed to pass assgin operations. (4) a new variable can only be generated in main memory. (5) Only one thread can lock a variable at a time. (6) If you lock a variable, the value of the variable will be emptied from the working memory. Before the execution engine can use the variable, you need to load or assgin the variable again to initialize it. (7) Do not unlock a variable that has not been locked. Do not unlock a variable that has been locked by another thread. (8) Before you unlock a variable, synchronize it back to the main memory

Memory partition of the heap:

The Java heap is divided into young generation, Old Memory, and Perm generation, as shown in the figure. In Jdk1.8, the permanent generation is removed and MetaSpace is used instead. 1, new generation: (1) Use Copinng algorithm (Copinng algorithm), because the young generation will recycle most objects every time GC. The New generation is divided into a larger Eden space and two smaller Survivor Spaces. Only Eden and one Survivor space are used at a time. Then, during garbage collection, the surviving object is placed into the unused Survivor space to clear Eden and the previously used Survivor space. (2) The default ratio is 8:1:1 for Eden, Survivor From, Survivor To. (3) Minor GC occurs when memory is insufficient. (1) The mark-compact algorithm is adopted, because in the old days only a small number of objects are reclaimed per GC. 3, Perm: used to store the metadata of the class, that is, the method area. (1) Perm repeal: in jdk1.8, Perm is replaced with MetaSpace, which is stored in local memory. The reason is that there is not enough memory in the permanent generation approach, or there is a memory leak. (2) MetaSpace: MetaSpace is similar in nature to the permanent generation, which is an implementation of the method area in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. 4, Heap memory partition in JVM schematic:

GC garbage collection:

1. Accessibility analysis method: search through a series of “GC Roots” objects as the starting point. If there is no reachable path between “GC Roots” and an object, the object is said to be unreachable. Unreachable objects do not necessarily become recyclable. A thread that has gone DEAD can also be recovered, and the GC does not reclaim its memory. (Some objects are treated as root objects, which the JVM considers non-recyclable, and which reference objects are also non-recyclable.) (1) Objects referenced in virtual machine stack (local variable table in stack frame) (2) objects referenced by static attributes in method area (3) objects referenced by constants in method area (4) Objects referenced by Native methods in local method stack 3. When an object is judged to be recoverable, it needs to go through two stages: (1) The first stage is reachability analysis, analyzing whether the object is reachability. (2) The second stage is when the object does not rewrite Finalize () method or Finalize () method has been called, the virtual machine thinks that the object can not be saved, so it retrieves the object. Garbage collection in the method area: (1) If some constant and symbol references in the constant pool are not referenced, they will be cleared out of the constant pool. (2) Useless classes: classes that are judged to be useless will be cleared out of the method area. Finalize (): (1) When GC garbage collection wants to collect an object, call the Finalize () method of the object. The object’s memory is then reclaimed at the next garbage collection. (2) In this method, you can specify some objects must perform operations before the release.

Virtual machine full GC frequently Jstat — gccause process id (2) if system.gc (), See where the code calls this method (3) if it’s heap Inspection, where the jmap — histo[:live] command is executed, and (4) if it’s GC Locker, the JNI library that the program relies on

(1) Idea: The mark-sweep algorithm is divided into two stages: the marking stage and the cleaning stage. The task of the marking stage is to mark all the objects that need to be reclaimed. The clearing stage is to clear the space of the marked objects. Idea: Divide the available memory into two pieces of equivalent size and use only one of them at a time. When it’s time for garbage collection, copy all the living objects into another block, and then empty the used memory space at once. (2) Advantages and disadvantages: it is not easy to generate memory fragmentation; Less available memory space; If there are many living objects, it is inefficient. (1) Idea: Mark the living object first, then move the living object to one side, and then clean up the memory beyond the end boundary. (2) Advantages and disadvantages: it is not easy to generate memory fragmentation; High memory utilization; When there are many living objects and they are scattered, the number of movement is high and the efficiency is low

4. Generational collection algorithm (currently used by garbage collectors in most JVMS) :

Idea: Divide the heap into new generation and old generation. (Permanent generation refers to method area)Copy the code

(1) Since the new generation recycles most of its objects each time, it uses a Copying algorithm. The New generation is divided into a larger Eden space and two smaller Survivor Spaces. Only Eden and one Survivor space are used at a time. Then, during garbage collection, the surviving object is placed into the unused Survivor space to clear Eden and the previously used Survivor space. (2) Since only a small number of objects are recovered each time in the old era, the Mark-Compact algorithm is adopted. (3) There is a permanent generation outside the heap area. The collection of persistent generation is mainly for invalid classes and constants. Garbage collection can affect program performance. The Java virtual machine must track useful objects in a running program and then release useless objects. This process consumes processor time.

A. The more likely it is to trigger A Minor GC when the JVM is unable to allocate memory for A new object. So the higher the allocation rate, the less memory it will have, the more frequently it will perform Minor GC operations without affecting the permanent generation (Tenured). References from the permanent generation to the young generation are treated as GC Roots, and references from the young generation to the old generation are ignored in the marking phase.Copy the code

(2) Major GC: Clean up the whole old age, triggered when Eden is out of memory. (3) Full GC: Clears the entire heap space, including young generation and old generation. Triggered when old age is out of memory

HotSpot VIRTUAL Machine details:

(1) When the virtual machine gets a new instruction, it first checks whether the parameter of the instruction can locate a symbolic reference of a class in the constant pool, and checks whether the class represented by this symbolic reference has been loaded, connected, and initialized. If not, the class is loaded. (2) Allocate memory for this object. A. Assume that the Java heap is neat, with all used memory on one side and free memory on the other, and A pointer in the middle as an indicator of the cut-off point. This allocation is called “pointer collisions.” B. If the Java heap is not tidy, and used memory and free memory cross each other, there is no way to “pointer collisions.” The virtual machine maintains a list of which memory blocks are available, finds a large enough space to allocate to object instances at allocation time, and updates the records on the table. This type of allocation is called a “free list”. C. The allocation method used depends on whether the Java heap is tidy. Whether the Java heap is tidy or not depends on whether the garbage collector adopted has collation capabilities. D. Allocation of objects to ensure thread safety: The VIRTUAL machine uses the CAS failure retry mode to ensure atomicity of update operations. There is actually another option: each thread preallocates a small chunk of memory in the Java heap, called a local thread allocation buffer, TLAB. Whichever thread wants to allocate memory is allocated on the TLAB of the thread, and synchronization locking is only performed when the TLAB runs out and new TLabs are allocated. Whether the VM uses TLAB is determined by the -xx :+/ -usetlab parameter. (3) The unallocated memory space of the VM is initialized to zero (the default value). (4) The VM performs necessary Settings on objects. For example, which class the object is an instance of, how to find metadata information about the object, the Hash code of the object, the GC generation age of the object, and so on. This information is stored in the object header of the object. (5) Execute the method to initialize the object according to the programmer’s wishes. (1) Handle: With the handle method, the Java heap will be divided into a block of memory as a handle pool, reference is stored in the address of the object handle. The handle contains the address of the object instance data and object type data.

(2) Direct pointer: In the way of direct pointer, reference stores the address of the object. The layout of Java heap objects must consider how object type data is accessed.

(3) Both methods have their own advantages: A. The advantage of using handle access is that the reference stores A stable handle address. When the object is moved (for example, when the object is moved during garbage collection), only the instance data pointer in the handle is changed, and the reference itself is not modified. B. Direct pointer is used to save the time cost of pointer positioning. (1) How does HotSpot find GC Root quickly? HotSpot uses a set of data structures called OopMap. Once the class is loaded, HotSpot calculates what type of data is at what offset in the object, and where references are placed in the stack and registers during JIT compilation. This way, during GC scanning, you can directly know which objects are reachable. (2) Safe points: A. HotSpot only generates OopMap at specific locations, which are called safe points. B. GC cannot be stopped anywhere during program execution, only when it reaches a safe point. C, the selection of the safety point is basically selected by the characteristics of “whether to let the program run for a long time”. Examples include method calls, loop jumps, exception jumps, and so on. Instructions with these capabilities produce Safepoint. (3) Interruption mode:

A, preemptive interrupt: when GC occurs, all threads are interrupted first. If any thread is found to be out of the safe point, the thread is recovered and allowed to run to the safe point. B. Active interrupt: when GC needs to interrupt a thread, it does not operate directly on the thread, but only sets a flag. Each thread takes the initiative to poll this flag when executing, and interrupts and suspends itself when it finds that the interrupt flag is true. Polling marks overlap with safety points.

(5) Safe zone: It is safe to start GC anywhere in a code fragment where the reference relationship of the object does not change. When a thread enters a safe zone, it first signals that it has entered the safe zone, and during this time, when the JVM initiates a GC, the thread entering the safe zone is left alone. When the thread is about to leave the safe zone, it checks to see if the system has completed the GC process, and if so, it moves on. Otherwise, it must wait until it receives a signal that it can leave the safe area. 4. Why pause all Java threads during GC? Because GC does reachability analysis first. Reachability analysis is to determine whether GC Root objects are reachability to other objects. If reference relationships of objects are constantly changing during analysis, the accuracy of analysis results cannot be guaranteed. CMS collector: (1) A collector whose goal is to obtain the shortest collection pause time. (2) Generally used in Internet sites or B/S system server (3) based on the implementation of mark-clear algorithm, but more complex, the whole process is four steps:

A. Initial tags: mark objects that GC Root can directly refer to B. Concurrent tags: Use multithreading to search for each GC Root object and search for all objects that can be associated with it in the heap. C, relabelling: in order to correct the concurrent marking period, the user program continues to operate, resulting in changes in the marking of the part of the object record. D. Concurrent cleanup: use multiple threads to clear marked objects

(4) Since the longest concurrent markup and concurrent cleanup operations are performed by the user thread, in general, the CMS memory reclamation work is performed concurrently with the user thread. (5) Disadvantages:

A. Occupy A lot of CPU resources. The application may be slow to respond because it consumes some CPU resources. B. CMS cannot handle floating garbage. During the concurrent cleanup phase, the user program continues to run and may generate new memory garbage that appears after the marking process and therefore cannot be removed by CMS. This part of garbage is called “floating garbage” C. A portion of memory needs to be reserved for use by user programs during garbage collection. D. Based on mark-clear algorithm, it is easy to generate a large amount of memory fragmentation, leading to full GC (full GC defragmentation)

6. Memory layout of the object header: HotSpot’s object header is divided into two parts. The first part is used to store the runtime data of the object itself, such as hash code, GC generation age, etc. The other part is used for Pointers to object type data in the method area. Bias locks: Bias locks favor the first thread to acquire it. If no other thread acquises the lock during subsequent execution, the thread holding the bias lock never needs to synchronize. When a thread acquises a bias lock, each time it enters the lock’s associated synchronization block, the virtual machine does not perform any synchronization operations. The bias mode ends when another thread attempts to acquire the lock.

The JVM optimization:

1. Generally speaking, when the survivor zone is not large enough or the occupancy reaches 50%, some objects will be placed in the old zone. By setting a reasonable Eden zone, survivor zone, and usage, you can save young objects in the young generation to avoid full GC, and use -xmn to set the size of the young generation

2. For large objects that occupy a lot of memory, memory will be allocated in the old age. If you allocate memory for large objects in the young generation and the young generation runs out of memory, you will have to move a large number of objects in Eden to the old age, and then the moved objects may die quickly, resulting in full GC. By setting the following parameter: -xx :PetenureSizeThreshold=1000000 (unit: B). It indicates that memory space will be allocated in tenured years when the size of an object exceeds 1M.

3. Under normal circumstances, young objects will be placed in Eden zone. After the first GC, if the object is still alive, it will be placed in Survivor zone. This threshold can be isomorphic to the -xx :MaxTenuringThreshold setting. If you want to keep objects in the young generation, you can set a large threshold.

Set minimum heap and maximum heap: Heap garbage collection is advantageous. The way to get a stable heap size is to set the values of -xms and -xmx to be the same, i.e. the maximum heap and the minimum heap. If this is set, the system will theoretically have a constant heap size at runtime. Many servers set these two parameters to the same value. A stable heap size, while reducing the number of GCS, increases the time of each GC, because each GC keeps the heap size within a range.

An unstable heap is not useless. When the system does not need to use large memory, the heap space is compressed so that the GC handles one smaller heap space at a time, speeding up the number of GC sessions. With this in mind, the JVM provides two parameters to compress and expand the heap space. (1) -xx :MinHeapFreeRatio Sets the minimum free ratio of the heap space. The default value is 40. When the ratio of free memory to heap space is less than 40, the JVM expands the heap space (2). -xx :MaxHeapFreeRatio sets the maximum ratio of free memory to heap space. The default value is 70, and when the ratio of free memory to heap space is greater than 70, the JVM compresses the heap space. (3) If -xmx and -xmx are equal, the above two parameters are invalid

6. Improve system performance by increasing throughput by setting up a parallel garbage collector. (1) -xx :+UseParallelGC: The young generation uses the parallel garbage collection collector. This is a throughput-focused collector that minimizes garbage collection time. (2) -xx :+UseParallelOldGC: Set the old age to use the parallel garbage collection collector.

7, try to use large memory pages: use large memory pages to increase the CPU’s memory addressing capacity and thus system performance. -xx :+LargePageSizeInBytes Sets the size of the memory page

8. Use a non-occupied garbage collector. -xx :+UseConcMarkSweepGC Used the CMS collector to reduce pauses in the old days.

-xxsurvivorRatio =3, which indicates the allocation ratio of young generations: survivor: Eden = 2:3

10, JVM performance tuning tools: (1) JPS (Java Process Status) : output Status information of the Process running in the JVM (jConsole is generally used now) (2) JStack: view stack information of the threads in the Java Process. (3) JMAP: used to generate heap dump snapshots (4) JHAT: used to analyze heap dump snapshots generated by JMAP (generally not recommended, using Ecplise Memory Analyzer instead) (3) JSTAT is a JVM statistical monitoring tool. It can be used to display garbage collection information, class loading information, new generation statistics, etc. (4) VisualVM: Fault processing tool

Class loading mechanism:

The class loader reads the binary data from the class file into memory, stores it in the method area, and then creates a java.lang. class object in the heap that encapsulates the data structure of the class in the method area. The steps of class loading are as follows: 1. Load: search and load the binary data of the class (load the information in the class file into memory) 2. Connect: merge the binary data of the class in memory into the running environment of the VIRTUAL machine (1) Verify: ensure the correctness of the loaded class. Include:

A. Structural check of class files: check whether they meet the fixed format of Java class files; B. Semantic check: ensure that the class itself conforms to the syntax specification of Java; C. Bytecode verification: ensure that the bytecode stream can be safely executed by Java virtual machines. A bytecode stream is a sequence of opcodes. Each opcode is followed by one or more operands. Bytecode checking This step checks whether each opcode is valid. D, binary compatibility verification: to ensure compatibility between classes that reference each other.

(2) Prepare: allocate memory for class static variables and initialize them to default values. Put the class into direct reference symbols referenced in a (such as method of symbol references, is made up, the method name and relevant descriptors in the parsing stage, the JVM reference symbols replaced with a pointer, the pointer is the direct reference, it points to the class of the method in the area of the memory location) 3, initialization: as a class of static variables gives the right initial value. When the value to the right of the equal sign of a static variable is a constant expression, the static code block is not called for initialization. Static initialization is called only if the value to the right of the equals sign is a run-time computed value.

1. When a classloader receives a classload request, it does not load the information about the class itself, but forwards the request to the parent classloader, up the hierarchy. So all class loading requests are passed to the parent class loader, and only if the parent class loader fails to load the required class will the subclass loader attempt to load the class itself. Throws a ClassNotFindException when neither the current class loader nor any parent class loader is able to load the class. 2. Significance:

Improve system security. User-defined class loaders cannot load reliable classes that should be loaded by the parent loader. (For example, if a user defines a malicious code, the custom class loader first asks the system loader to load it. The system loader checks that the code does not conform to the specification and stops loading it.)

Defining class loaders: If a class loader can load a class, it is called defining class loaders. Initial class loaders: Defining class loaders and all their children are called initial class loaders. 5. Runtime package: (1) the runtime package consists of classes loaded by the same classloader and have the same package name. (2) Only classes belonging to the same runtime package can access classes and class members visible to the package (default). It prevents a user – defined class from impersonating a core class from accessing the package visible members of the core class library. 6. Loading two copies of the same class object: A and B are not parent-child classloaders and each has loaded the same class.

1. Full responsibility: When a class loader loads a class, other classes that the class depends on are also loaded into memory by the class loader. 2. Cache mechanism: All Class objects are cached. When a program needs to use a Class, the Class loader first looks for the data in the cache.

Class loaders: there are two types of class loaders: 1. The JVM native class loaders (3 types) : A, c + + written, programmers can’t in the program for the class b, is responsible for loading the virtual machine core library, such as Java. Lang. Object c, not inherit this class (2) the Extension class loader (Extension) : C) is a subclass of ClassLoader. D) Is a subclass of ClassLoader. If the user creates a JAR file in the specified directory, it will be loaded by the extension loader. (3) System loader or application loader (App) : a, written in Java B, the parent loader is an extension class loader C, loads classes from environment variables or class.path D, is the default parent loader of user-defined class loading E, is a subclass of ClassLoader

2. User-defined class loaders: (1) subclasses of java.lang.ClassLoader class (2) users can customize the loading method of the class (3) the parent ClassLoader is the system loader (4) writing steps: A, inherit ClassLoader B, rewrite findClass method. Load a class file from a specific location, get a byte array, and use defineClass to convert the byte array into a class object. (5) Why custom class loaders? A, can load A class file from A specified location, such as A database, cloud load class file B, encryption: Java code can be easily decompiled, so if the code needs to be encrypted, then the encrypted code cannot use Java’s own ClassLoader to load the class. Instead, you need to customize the ClassLoader, decrypt the class, and load it.

Problem: Java programs can execute classes in several ways: 1. Active use (6 cases) : The JVM must initialize each class on its “first active use.” (1) Create an instance of a Class (2) read and write a static variable of a Class or interface (3) call a static method of a Class (4) fetch a Class from a reflected API (class.forname ()) (5) initialize a subclass of a Class (6) When the JVM starts, Classes that are marked as initiators (classes that contain the Main method) can be considered active use of the class or interface only if the static variable or method used by the program is actually defined in the class. 2. Passive use: Except for six cases of active use, all cases are passive and do not result in class initialization. 3. The JVM specification allows classloaders to preload a class in anticipation that it will be used. If the class file is missing or has an error, the error is reported only when the program “first uses it actively.” Linkage Error. If the class has not been “actively used” by the program, no error is reported.

Class loading mechanism and interfaces: 1. When a Java VIRTUAL machine initializes a class, the interface implemented by the class is not initialized. 2. When initializing an interface, the parent interface of the interface is not initialized. 3. The interface is initialized only when the program first uses a static variable of the interface.

ClassLoader: 1. Call the loadClass method of ClassLoader to load a class. It is not used actively, so class initialization is not performed.

Classes loaded by the JVM’s three native loaders (root, extension, system) are never unloaded. Because the JVM always references these classloaders, which use references to the classes they load, these Class objects are always reachable. 2. Classes loaded by user-defined class loaders can be unloaded.

Supplement:

  • The JDK and JRK

(1) JDK: Java Development Kit, used in the Development of the class package. (2) JRE: Java Runtime Environment, the basis of Java running, contains all the class libraries required by the Runtime.

  • Diagram Java files into machine code

The JVM virtual machine compiles Java files into class files (bytecode files) and then converts class files into machine instructions that all operating systems can run.

Source: segmentfault.com/a/119000001…