Java memory model
How does the Java code we developers write make it known to the computer
-
First of all, the computer is a binary system. He only knows 01010101
-
For example, we often write HelloWord. Java how does the computer know how to run
-
HelloWord. Java is written by our programmers, we can know, but the computer does not know
Java file compilation process
- Programmer prepared. Java file
- Compile to bytecode files from javac. Class :(why compile to class files, because the JVM only knows.class files)
- When compiled by the JVM into files that a computer knows (files are everything to a computer system)
(This is a general concept of abstract painting)
Why is Java a cross-platform language
- This kua platform is the kua platform for intermediate language (JVM) implementations
- Java has the JVM to mask the underlying hardware from the software level, and the details of the instruction level make it compatible with various systems
C and C++ need to be compatible with different operating systems at the compiler level. Those who have written C and C++ know that some of the code in different operating systems is different
The difference between Jdk and Jre and JVM
-
The Jdk includes both the Jre and the Jvm, and the Jre includes the Jvm
-
The Jdk is the development kit we use to write code
-
Jre is the Java runtime environment, which is mostly written in C and C++. It is the basic class library that we need when compiling Java
-
The Jvm, commonly known as the Java Virtual Machine, is part of the Java runtime environment. It is a fictitious computer that implements Java applications by emulating various computer functions on the actual computer
-
Look at the official picture of Java, Jdk includes Jre, Jre includes JVM
What are the parts of the JVM and what is the running process?
-
The JVM consists of two subsystems and two components: the Class loader and the Execution engine. The two components are Runtime data area and Native Interface.
-
Class loader: loads the Class file into the Method Area of the Runtime data area based on the given fully qualified Class name (for example, java.lang.object).
-
Execution Engine: Executes instructions in classes.
-
Native Interfaces: Interact with Native libraries. Native interfaces are used for interaction with other programming languages.
-
Runtime data area: This is what we call the memory of the JVM.
-
-
Process: The compiler converts Java code into bytecode, and the ClassLoader loads the bytecode into memory and places it in the methods section of the Runtime data area. Bytecode files are just instructions set specifications for the JVM. It cannot be delivered directly to the underlying operating system, so a specific command parser Execution Engine is required to translate bytecode into the underlying system instructions for Execution by the CPU. In this process, it is necessary to call the Native Interface of other languages to realize the function of the whole program.
Talk about the JVM runtime data area
- During the execution of Java programs, the Java VIRTUAL machine divides the memory area managed by the virtual machine into several data areas. Each of these areas has its own purpose and time of creation and destruction. Some areas exist with the start of a virtual machine process, while others are created and destroyed depending on the start and end of a thread. The memory managed by the Java VIRTUAL machine is divided into the following areas:
Simply put, that's where our Java runtime stuff is
-
Program Counter Register: The bytecode parser’s job is to select the next bytecode instruction to be executed by changing the value of this counter. Branch, loop, jump, exception handling, thread recovery and other basic functions need to be completed by this counter.
Why thread counters? Because threads don't have memory
-
Java Virtual Machine Stacks: When each method is executed, a Stack Frame is created in the Java Virtual Machine Stack to store information about local variables, operand Stacks, dynamic links, method exits, etc.
A stack frame is the next unit in the Java virtual machine stack
-
Native Method Stack: Serves the same function as virtual machine Stack, except that the virtual machine Stack serves Java methods, while the local Method Stack serves Native methods for virtual machines.
The Native keyword modification method is not seen, the source code of Native method is mostly C and C++ code
-
The Java Heap is the largest area of memory in the Java virtual machine, which is shared by all threads and where almost all object instances are allocated memory.
-
Methed Area: Used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine.
The JVM runtime data area is explained in more detail later
What about the program counter? (Key understanding)
-
A program counter is a small memory space that can be thought of as: the address (line number) of the bytecode instructions being executed by the current thread
-
Because Java virtual machine multithreading is implemented by switching threads and allocating processor execution time, each processor will execute instructions in only one thread. Therefore, in order to restore the correct execution position after the thread switch, each thread has an independent program counter, counters between each thread do not affect each other, independent storage. This is called “thread private” memory. The program counter memory area is the only area in the virtual machine where OutOfMemoryError cases are not specified.
Summary: You can also call it a thread counter
-
Example: The smallest unit of execution in Java is a thread, and a thread executes instructions that ultimately operate on our computer, the CPU. Running on the CPU, there is a very unstable factor called the scheduling policy, and the scheduling policy is based on the time slice, that is, the current nanosecond is allocated to that instruction.
-
If:
-
Thread A is watching live
-
All of A sudden, thread B gets A video call, grabs thread A’s time slice, interrupts thread A, and thread A hangs
-
Then, the video call ends, and what exactly should thread A do? (Thread is the smallest unit of execution, it has no memory function, it is only responsible for doing, and this memory is recorded by: program counter)
-
How about the Java virtual machine stack? (Key understanding)
- The Java virtual machine is thread-private and has the same life cycle as a thread.
- The virtual stack describes the memory model of Java method execution:
Each method is executed at the same time
A Stack Frame is created to store information about local variables, operand stacks, dynamic links, method exits, etc.
- Explanation: The virtual machine stack has units, the unit is stack frame, a method a stack frame. In a stack frame he has to store local variables, operand stacks, dynamic links, exits, etc.
Parse stack frames:
- Local variable table: is used to store our temporary 8 basic data types, object reference addresses, and returnAddress types. (The returnAddress holds the instruction address of the bytecode to be executed after the return.)
- Operand stack: Operand stack is used for operations, such as code with I = 6*6, it will operate at the beginning, read our code, calculate, and put in the local variable table
- Dynamic links: If I have a service.add() method in my method that links to another method, this is where dynamic links are stored.
- Return = return; return = return
Does one method call another method create a lot of stack frames?
- A: Yes. If there is a dynamic link in a stack to call another method, a new stack frame will be created. The stack is ordered. One stack frame calls another stack frame, and the other stack frame will be placed below the caller
What does stack to heap mean?
- What does stack to heap mean, what do you do when you use a member variable in the stack, you don’t store a member variable in the stack, you just store an application address
Does the recursive call itself create a lot of stack frames?
- A: Recursively, multiple stack frames are also created, that is, all the way down the stack
Can you give me a detailed introduction to the Java heap? (Key understanding)
- The Java Heap is the largest chunk of memory managed by the Java VIRTUAL machine. It is an area of memory shared by all threads and created when the virtual machine is started. The sole purpose of this memory area is to hold object instances.
- The Java Virtual Machine specification says that all object instances and arrays are allocated on the heap.
- The Java heap is the primary area managed by the garbage collector and is therefore also referred to as the “GC heap.”
- The Java heap can be divided into the new generation and the old generation from a memory reclamation perspective.
- From a memory allocation perspective, a Java heap shared by threads can be partitioned into multiple thread private allocation buffers.
- No matter how to divide, it has nothing to do with the storage content, no matter which area, the storage is the object instance, further division is to better reclaim memory, or faster allocation of memory.
- According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space. Most current virtual machines are extensible (controlled by -xmx and -xMS). OutOfMemoryError is thrown if there is no memory in the heap for instance allocation and the heap cannot be extended.
Can you explain the local method stack?
- The native method stack is easy to understand, it is very similar to the stack, but the method with the native keyword stack
- It is a service method of the virtual machine stack that executes Java methods (that is, bytecode) for the virtual machine
- Native keyword method is not visible, you must go to oracle official website to download to see, and most of the source code modified by native keyword is C and C++ code.
- In the same way, the native method stack is C and C++ code
Can you explain the method area?
- The method area is an area of memory shared by all threads that stores data such as class information that has been loaded by the Java virtual machine, constants, static variables, just-in-time compiler compiled code, and so on.
- It has a special name, non-heap. OutOfMemoryError is thrown when the method area cannot meet memory allocation requirements.
What is the JVM bytecode execution engine
-
The core component of the virtual machine is the execution engine, which is responsible for the execution of the virtual machine bytecode, which is first compiled into machine code and then executed by ordinary users.
-
“Virtual machine” is a concept relative to “physical machine”, the virtual machine bytecode is not directly run on the physical machine, the JVM bytecode execution engine – compiled into machine code can be executed on the physical machine.
Have you ever heard of direct memory?
- Direct Memory is not part of the virtual machine’s run-time data region, nor is it defined in the Java Virtual machine. But this part of memory is also frequently used and can cause OutofMemoryErrors, so we’ll cover it here.
- My understanding is that direct memory is based on the intermediate memory between physical memory and Java virtual machine memory
Do you know the garbage collection system?
-
During the program running, a large amount of memory garbage will be generated (some memory objects that do not point to references belong to the memory garbage, because these objects are no longer accessible, the program can no longer use them, and they are dead to the program). In order to ensure the performance of the program running, The Java virtual machine continuously performs automatic garbage collection (GC) as the program runs.
-
Garbage collection system is the core of Java, is also indispensable, Java has its own garbage cleaning mechanism, developers do not need to manually clean up
-
Part of the reason Java is leading the market is the strength of Java garbage collection systems
What’s the difference between a stack?
contrast The JVM heap The JVM stack Physical address The heap’s physical address assignment is discontinuous to objects. So the performance is slower. Discontinuous allocations are also taken into account during GC, so there are various algorithms. For example, mark-erase, copy, mark-compress, generation (the new generation uses the copy algorithm, the old generation uses mark-compress) The stack uses the stack in the data structure, first in, last out principle, physical address allocation is continuous. So the performance is fast. Memory, respectively, Because the heap is discontinuous, the allocated memory is confirmed at run time and therefore the size is not fixed. The average heap size is much larger than the stack. The stack is contiguous, so the allocated memory size is determined at compile time and is fixed. Contents stored The heap holds instances and arrays of objects. Therefore, the area is more concerned with data storage Stack storage: local variables, operand stack, return results. The district is more concerned with the implementation of procedural methods. Program visibility The heap is shared and visible to the entire application. The stack is visible only to threads. So it’s thread private. Its life cycle is the same as that of a thread.
- Note:
- Static variables are placed in the methods area
- Static objects are left in the heap.
Deep copy and shallow copy
- ShallowCopy simply adds a pointer to an existing memory address,
- A deepCopy adds a pointer to a new memory and makes the pointer point to the new memory.
- Shallow copy: refers only to the memory address to be copied. If the original address changes, the shallow-copied object will change accordingly.
- Deep copy: The creation of a new memory location in the computer to store the copied object.
Does Java have memory leaks? Please explain why?
-
A memory leak is when objects or variables that are no longer in use remain in memory. In theory, Java has a GC garbage collection mechanism, which means that objects that are no longer in use are automatically reclaimed by the GC and erased from memory.
-
However, even then, Java still has memory leaks, and the causes of Java memory leaks are clear: A memory leak occurs when a long-lived object holds a reference to a short-lived object. Although a short-lived object is no longer needed, it cannot be reclaimed because a long-lived object holds a reference to it.
Garbage collection mechanism and algorithm
Describe the Java garbage collection mechanism
- In Java, programmers do not need to display to free the memory of an object, but the virtual machine does it itself. In the JVM, there is a garbage collection thread, which is low priority and does not execute under normal circumstances, but only when the VIRTUAL machine is idle or the current heap is out of memory, which sweeps through objects that are not referenced by any of them and adds them to the collection for collection.
What is GC? Why GC
- GC stands for garbage Collection (Gabage Collection). Memory processing is a problem prone to programmers. Forgetting or wrong memory Collection will lead to instability or even crash of the program or system. Java provides a GC facility that automatically detects if an object is out of scope to reclaim memory automatically. The Java language does not provide a display operation to free allocated memory.
Advantages and disadvantages of garbage recycling
-
Advantage: The JVM’s garbage collector does not require us to handle unreferenced objects manually, which is the biggest advantage
-
Disadvantages: Programmers cannot call the garbage collector for an object or all objects in real time.
How does garbage collector work? Is there any way to do garbage collection manually?
-
In the case of the GC, when a programmer creates an object, the GC starts monitoring the address, size, and usage of the object.
-
In general, GC records and manages all objects in the heap in a directed graph. This is a way to determine which objects are “reachable” and which are “unreachable”. When the GC determines that some objects are “unreachable”, it is the GC’s responsibility to reclaim those memory Spaces.
-
You can. Programmers can manually execute System.gc() to tell the GC to run, but the Java language specification does not guarantee that the GC will execute.
What reference types are available in the JVM?
- Strong references: No collection occurs when gc occurs.
- Soft references: useful but not required objects that are reclaimed before an overflow occurs.
- Weak references: Useful but not required objects that will be collected in the next GC.
- Virtual reference (ghost reference/PhantomReference) : objects cannot be obtained by virtual reference. Virtual reference is implemented by PhantomReference. The purpose of the virtual reference is to return a notification on gc.
How do I determine whether an object can be reclaimed?
-
Garbage collector in the garbage collection, the first need to determine which memory is to be recycled, which objects are alive, can not be recycled; Which objects are dead and need to be recycled.
-
There are two ways to judge:
- Reference counter method: create a reference count for each object, count +1 when there is an object reference, count -1 when the reference is released, and can be reclaimed when the counter is 0. It has a disadvantage that it does not solve the problem of circular references; (This has been eliminated)
- Reachability analysis algorithm: Search down from GC Roots, and the search path is called reference chain. When an object is not connected to GC Roots by any reference chain, the object is proven to be recyclable. (Very, very widely used in the market)
What is Full GC
- Clean up the entire heap space – both young and old generations and permanent generations
- Full GC is very slow to execute because it cleans up the heap, and it’s best to keep Full GC firing as little as possible in Java development
When can objects be collected by the garbage collector
- When an object becomes untouchable to the application that currently uses it, it can be reclaimed.
- Garbage collection does not occur in the permanent generation, and Full garbage collection (Full GC) is triggered if the permanent generation is Full or the threshold is exceeded. If you look closely at the output from the garbage collector, you will see that persistent generations are also collected. This is why the correct permanent generation size is important to avoid Full GC.
What are the JVM garbage collection algorithms?
- Mark-sweep algorithm: Marks unwanted objects and then collects them. Disadvantages: Inefficient, unable to remove garbage debris.
- Copy algorithm: divide two memory areas of equal size according to the capacity. When one area is used up, the living objects are copied to the other area, and then the used memory space is cleared once. Disadvantages: Low memory usage, only half of the original.
- Mark-declutter algorithm: Marks useless objects, moves all surviving objects towards one end, and then clears memory directly beyond the end boundary.
- Generation algorithm: according to the different life cycle of the object, the memory is divided into several blocks, generally the new generation and the old age. The new generation basically adopts the replication algorithm, and the old age adopts the tag sorting algorithm.
Mark-clear algorithm
-
Mark unwanted objects and then recycle them.
-
Mark-sweep is a common basic garbage collection algorithm that divides garbage collection into two phases:
- Mark stage: Mark objects that can be recycled.
- Cleanup phase: Reclaim space occupied by marked objects.
-
The mark-sweep algorithm is fundamental because all the garbage collection algorithms that follow are improved upon it.
-
Advantages: Simple implementation, no object to move.
-
Disadvantages: Inefficient marking and cleaning process, resulting in a large number of discontinuous memory fragments, improve the frequency of garbage collection.
-
The execution process of the mark-clear algorithm is shown below
Replication algorithm
-
In order to solve the problem of low efficiency of mark-clear algorithm, a copy algorithm was developed. It divides the memory space into two equal areas and uses only one of them at a time. Garbage collection iterates through the currently used region, copies live objects to another region, and finally collects recyclable objects from the currently used region.
-
Advantages: Memory can be allocated sequentially, simple implementation, efficient operation, do not consider memory fragmentation.
-
Disadvantages: The available memory size is reduced by half, and objects are frequently copied when they have a high survival rate.
-
The execution process of the replication algorithm is shown in the following figure
Mark-collation algorithm
-
Replication algorithm can be used in the new generation, but cannot be selected in the old age, because the survival rate of objects in the old age will be higher, so there will be more replication operations, resulting in low efficiency. Mark-clear algorithm can be used in the old era, but it is not efficient, and it is easy to generate a large number of memory fragments after memory reclamation. Therefore, a mark-compact algorithm emerged. Unlike mark-compact algorithm, after marking the recyclable objects, all the surviving objects are compressed to one end of memory, so that they are Compact together, and then the memory beyond the end boundary is reclaimed. After collection, used and unused memory are on separate sides.
-
Advantages: It solves the memory fragmentation problem of mark-clean algorithm.
-
Disadvantages: Local object movement is still needed, which reduces efficiency to a certain extent.
-
The execution process of the mark-collation algorithm is shown in the figure below
Generational collection algorithm
- Currently, all commercial VMS use it
Generational collection
Garbage collection algorithm. Generational collection algorithms, as the name implies, are based on objectsLive cycle
Divide memory into blocks. Generally includeThe young generation
,The old s
andThe permanent generation
, as shown in the figure:(More on that later)
Does garbage collection occur in permanent generations in the JVM
- Garbage collection does not occur in the permanent generation, and Full garbage collection (Full GC) is triggered if the permanent generation is Full or the threshold is exceeded. If you look closely at the output from the garbage collector, you will see that persistent generations are also collected. This is why the correct permanent generation size is important to avoid Full GC. See Java8: From permanent generation to metadata.
Garbage collector and new generation, old generation, permanent generation
Let’s talk about the difference between the new generation, the old generation and the permanent generation
-
In Java, the heap is divided into two distinct regions: Young and Old. The Young generation is divided into three regions: Eden, From Survivor and To Survivor. The purpose of this partition is to enable the JVM to better manage objects in heap memory, including memory allocation and reclamation.
-
In the new generation, new objects are generally preserved, so a large number of objects are found dead and only a small number of objects survive each garbage collection, so the replication algorithm is adopted, and only a small amount of the replication cost of the surviving objects can be collected.
-
In the old age, generally preserved objects that have survived for a long time, they have a high survival rate and no extra space to allocate guarantees for it, must adopt the “mark-clean” or “mark-tidy” algorithm.
-
The permanent generation is the method area of the JVM. Here are some virtual machine loaded class information, static variables, constants and other data. Things in this area are much harder to recycle than older and newer generations.
What are Minor GC, Major GC, and Full GC
- Minor GC refers to garbage collection that occurs in the new generation. Since Java objects are mostly ephemeral, Minor GC is frequent and generally fast. (Generally, garbage is recycled by replication algorithm)
- A Major GC is an old GC, which refers to a GC that occurred in an old era, and it is common to execute a Major GC in conjunction with a Minor GC. Major GC is much slower than Minor GC. (Mark clear method and mark finishing method can be used)
- Full GC cleans the entire heap space, including young and old generations
Minor, Major, and Full GC differences and trigger conditions
-
The Minor GC triggers the following conditions:
- MinorGC is triggered when Eden is full. That is, when applying for an object, the Eden area is found to be insufficient, then MinorGC will be triggered once.
- Minor GC is triggered when the size of the newly created object is greater than the space left for Eden
-
Major GC and Full GC trigger conditions: Major GC is usually equivalent to Full GC
-
Average size of objects promoted to old age per promotion > old age remaining space
-
The number of surviving objects after MinorGC exceeds the remaining space of the old age
-
The permanent generation space is insufficient
-
Perform System. The gc ()
-
CMS GC abnormal
-
Heap memory allocates large objects
-
Why should the New generation be divided into Eden and two Survivor regions?
- If there is no Survivor, every time a Minor GC is performed in Eden, the surviving object is sent to the old age. The old generation fills up quickly, triggering a Major GC. The memory space of the old generation is much larger than that of the new generation, and a Full GC takes much longer than a Minor GC, so it needs to be classified into Eden and Survivor.
- Survivor exists in order to reduce the number of objects sent to the old age and thus the occurrence of Full GC. The pre-screening of Survivor guarantees that only objects that survive 15 Minor GC cycles will be sent to the old age.
- The biggest benefit of setting two Survivor zones is that fragmentation is solved. After a Minor GC is performed on the newly created object in Eden, the surviving object in Eden will be moved to the first Survivor space S0 and Eden will be emptied. When Eden is full, a Minor GC is triggered again, and the surviving objects in Eden and S0 are copied into S1, the second survivor space. (This is important because the replication algorithm ensures that the surviving objects in S1 from S0 and Eden take up contiguous memory space. Avoid fragmentation)
What is the default ratio of Old to Young Java heap?
-
By default, the ratio of Young to Old is 1:2 (specified by the parameter -xx :NewRatio), that is, Young = 1/3 of the heap size. Old = 2/3 heap size.
-
Among them, Young is subdivided into Eden and two Survivor areas, and the ratio of Edem and two Survivor areas is = 8:1:1 (which can be set by parameter — XX:SurvivorRatio).
-
However, the JVM only uses Eden and one of the Survivor regions at a time to serve objects, so at any given time one of the Survivor regions is always free.
Why do we divide generations like this:
-
In fact, the main reason is that object partitioning can be stored according to the characteristics of each generation, which is more convenient for recycling. The most appropriate collection algorithm is adopted:
-
In the new generation, a large number of objects are found dead and only a small number of objects survive in garbage collection, so the replication algorithm is adopted, and only a small amount of the replication cost of the surviving objects can be collected.
-
In the old age, because the object has a high survival rate and there is no extra space to allocate it, it must use “mark-clean” or “mark-tidy” algorithm.
-
-
The New generation is divided into Eden and Survivor (From and To, referred To as one region). Those are the three districts in the old days. The data will be allocated to the Eden region first (except for large objects, which are Java objects that require a large amount of continuous memory). The JVM is triggered to issue a Minor GC when Eden runs out of space. If an object survives one minor-GC and is accepted by a Survivor space, it is moved to a Survivor space. Each time Survivor survives the Minor GC, the age increases by 1. When Survivor reaches a certain age (default: 15), the age of the Survivor is promoted to the middle of the old age, but the age of the old age can be set.
What is a garbage collector and how is it different from a garbage algorithm
- The garbage collector is a concrete implementation of garbage collection algorithms (token-clean, token-collation, copying, generational), and the garbage collectors provided by different garbage collectors and different versions of JVMS may vary considerably.
What garbage collectors does the JVM have?
- If the garbage collection algorithm is the methodology of memory collection, then the garbage collector is the concrete implementation of memory collection. The figure below shows 7 collectors that operate on different generations, including the Serial, PraNew, and Parallel Avenge collectors, and the Serial Old, Parallel Old, AND CMS collectors. There is also a G1 collector for recycling the entire Java heap. The wiring between different collectors indicates that they can be used together.
Garbage collector The working area Recovery algorithm The worker thread User thread parallelism describe Serial New belt Replication algorithm Single thread no In Client mode, the new generation collector is default. A simple and efficient ParNew New belt Replication algorithm multithreading no Multi-threaded version of Serial, preferred in Server mode, can be paired with CMS’s new generation collector Parallel Scavenge New belt Replication algorithm multithreading no The goal is to achieve manageable throughput Serial Old Elderly with Mark-tidy Single thread no Serial An older version, used by VMS in Client mode Parallel Old Elderly with Mark-tidy multithreading no Parallel insane, throughput priority G1 Freshman belt + senior belt Mark-tidy + copy algorithm multithreading is JDK1.9 default garbage collector
- Serial collector (copy algorithm): the new generation of single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient;
- ParNew collector (copy algorithm): A new generation of delegate parallel collector, which is actually a multi-threaded version of Serial collector and performs better than Serial on multi-core CPUS;
- The Parallel Collector. A new generation of Parallel collectors, driven by the pursuit of high throughput and efficient CPU utilization. Throughput = user thread time /(user thread time +GC thread time), high throughput can efficiently use THE CPU time, as soon as possible to complete the calculation tasks of the program, suitable for background applications such as the corresponding interaction requirements are not high scenes;
- Serial Old collector (mark-collation algorithm): Old single-threaded collector, older version of Serial collector;
- The Parallel Old Collector (mark-collation algorithm) : the Parallel Collector, throughput first, older version of the Parallel Avenge collector;
- CMS(Concurrent Mark Sweep) collector (mark-sweep algorithm) : The old parallel collector aims at obtaining the shortest collector pause time. It has the characteristics of high concurrency and low pause, and pursues the shortest COLLECTOR pause time.
- G1(Garbage First) collector
Tag collation + copy algorithm to recycle garbage
) : Java heap parallel collector, G1 collector is a new collector provided in JDK1.7. The G1 collector is implemented based on a “mark-collation” algorithm, that is, it produces no memory fragmentation. In addition, the G1 collector differs from its predecessors in that it collects the entire Java heap (including the new generation and the old generation), whereas the first six collectors only collect the new generation or the old generation.
The collector can be allocated like this, right? (It’s good to know)
Serial / Serial Old
Serial / CMS
ParNew / Serial Old
ParNew / CMS
Parallel Scavenge / Serial Old
Parallel Scavenge / Parallel Old
G1
Copy the code
What are the new generation garbage collector and the old generation garbage collector? What’s the difference?
- Cenozoic recyclers: Serial, ParNew, Parallel Insane
- Old collector: Serial Old, Parallel Old, CMS
- Whole heap collector: G1
The new generation garbage collector generally adopts replication algorithm. The advantage of replication algorithm is high efficiency, but the disadvantage is low memory utilization. Old-time recyclers generally use a mark-collation algorithm for garbage collection.
Describe how generational garbage collector works.
-
The generation collector has two partitions: old generation and new generation. The default space of the new generation is 1/3 of the total space, and the default space of the old generation is 2/3.
-
The new generation uses the replication algorithm, and there are three partitions in the new generation: Eden, To Survivor, and From Survivor. Their default ratio is 8:1:1. Its execution process is as follows:
- Put Eden + From Survivor into To Survivor zone;
- Clear Eden and From Survivor partitions;
- Swap From Survivor and To Survivor partitions, From Survivor To Survivor, To Survivor To Survivor From Survivor.
-
An object that survives every move From Survivor To Survivor is aged +1 and is upgraded To the old generation when age reaches 15 (the default is 15). Large objects also go directly to old generation.
-
The old generation triggers global garbage collection when the footprint reaches a certain value, usually using a mark-up execution algorithm. These iterations constitute the entire implementation process of generational garbage collection.
Memory allocation policy
Briefly describe the Java memory allocation and reclamation policy and the Minor and Major GC
-
The so-called automatic memory management, ultimately to solve the two problems of memory allocation and memory reclamation. Earlier we talked about memory reclamation, so let’s talk about memory allocation.
-
Objects are usually allocated on the Java heap (and in some cases on the stack with the advent of virtual machine optimization, more on this later). Objects are mainly allocated in the Eden region of the new generation. If local thread caching is enabled, it is allocated on the TLAB according to thread priority. In rare cases, it will be allocated directly over the old age. In general, allocation rules are not 100% fixed, and the details depend on the combination of garbage collectors and the parameters of the virtual machine, but there are several “universal” rules for allocating memory for virtual machines:
Objects are allocated in Eden area first
- In most cases, objects are allocated in the New Generation Eden area. When the Eden allocation does not have enough space to allocate, the virtual machine will issue a Minor GC. If there is still not enough space after this GC, the allocation guarantee mechanism is enabled to allocate memory in the old years.
- Here we mention Minor GC, and if you look closely at the GC routine, we can usually see Major GC/Full GC in the log.
- Minor GC refers to GC that occurs in the new generation, because Java objects are mostly ephemeral, and all Minor GC is very frequent and generally very fast;
- Major /Full GC is a GC that occurs in an older era, and a Major GC is usually accompanied by at least one Minor GC. A Major GC is typically 10 times slower than a Minor GC.
Why do big objects go straight to the old age
-
Large objects are objects that require a large amount of contiguous memory space, and frequent occurrences of large objects can be fatal, causing GC to be triggered prematurely to obtain contiguous enough space to place new objects when there is not much left in memory.
-
As we mentioned earlier, the new generation uses a mark-sweep algorithm to handle garbage collection. If large objects are allocated directly in the new generation, a large amount of memory replication will occur between Eden and two Survivor regions. So large objects are allocated directly in the old age.
Long-lived objects will enter the old age
- Virtual machines use the idea of generational collection to manage memory, so the memory collection must determine which objects should be placed in the new generation and which objects should be placed in the old age. Therefore, the virtual machine defines an object age counter for each object. If the object is born in Eden and can be accommodated by Survivor, it will be moved to Survivor, and the object age is set to 1. Each time an object “survives” a Minor GC in a Survivor zone, the age increases by 1, and when it reaches a certain age (15 by default), it is promoted to the old age.
Vm class loading mechanism
What is the Java class loading mechanism?
- The virtual machine loads the data describing the Class from the Class file into memory, verifies, parses, and initializes the data, and eventually forms Java types that the virtual machine can use directly.
Mechanism and process of class loading
- When a program actively uses a class that has not already been loaded into memory, the JVM initializes the class through three steps: load, connect, and initialize. If there are no accidents, the JVM will complete three steps in a row, so these three steps are sometimes referred to collectively as class loading or class initialization.
1, load,
-
Loading refers to reading a class’s class file into memory, converting this static data into a runtime data structure in the method area, and generating a java.lang. class object in the heap that represents the class as an access point to the method area’s class data. This process requires the participation of the class loader.
-
Java class loaders are provided by the JVM and are the basis on which all programs run. These class loaders provided by the JVM are often referred to as system class loaders. In addition, developers can create their own classloaders by inheriting the ClassLoader base class.
-
Class loaders can load binary data of classes from different sources, such as: local Class files, Jar package Class files, network Class files, and so on.
-
The end product of Class loading is a Class object in the heap (not the target Class object) that encapsulates the Class’s data structure in the method area and provides the user with an interface to access the method area data structure, the Java reflected interface
2. Connection process
- When the Class is loaded, a corresponding Class object is generated for it, and the connection phase is then entered, which is responsible for merging the binary data of the Class into the JRE (meaning merging the binary code of the Java Class into the JVM’s running state). Class join can be divided into the following three stages.
-
Validation: To ensure that the loaded class information complies with the JVM specification and that there are no security issues. It mainly verifies whether it conforms to the Class file format specification and whether it can be loaded by the current virtual machine.
-
Preparation: The stage of formally allocating memory and setting initial values for class variables (static variables), which will be allocated in the method area
-
Resolution: symbolic references to the virtual machine constant pool are replaced by byte-reference procedures
3. Initialization
-
The initialization phase is the process of executing the class constructor
() method. The class constructor < Clinit >() method is generated by combining the assignment actions of all class variables in the class automatically stored by the compiler with statements in the static block, which is executed from top to bottom.
-
When initializing a class, if the parent class has not been initialized, the initialization of the parent class must be triggered first
-
The virtual machine ensures that a class’s
() methods are locked and synchronized correctly in a multithreaded environment
To sum up, initialization is assigning the correct initial value to a static variable of a class
Describe how the JVM loads Class files
-
All classes in Java need to be loaded into the JVM by the class loader to run. The class loader is itself a class, and its job is to read the class file from the hard disk into memory. When writing programs, we hardly need to care about class loading because it is implicitly loaded, unless we have special uses, such as reflection, that require explicit loading of the required class.
-
There are two types of class loading:
-
1. Implicit loading. When the program encounters an object generated through new or other methods during the running process, it implicitly calls the class loader to load the corresponding class into the JVM.
-
2. Explicitly load the required classes using methods such as class.forname()
-
-
Java classes are loaded dynamically. Instead of loading all classes at once and then running them, the underlying classes (like the base classes) that the program runs on are fully loaded into the JVM, and other classes are loaded as needed. This, of course, is to save memory overhead.
What is a class loader, and what are class loaders?
-
A block of code that implements retrieving the binary stream of a class by its permission name is called a classloader.
-
There are four main types of loaders:
- The Bootstrap ClassLoader (Bootstrap ClassLoader) is used to load Java core class libraries and cannot be directly referenced by Java programs.
- Extensions Class Loader: It is used to load Java extension libraries. The Implementation of the Java Virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory.
- System Class Loader: It loads Java classes based on the CLASSPATH of the Java application. In general, Java application classes are loaded by it. Through this. GetSystemClassLoader () to get it.
- User-defined class loaders are implemented by inheriting java.lang.ClassLoader classes.
What about the execution of class loading?
- Class loading is divided into the following five steps:
- Load: according to the search path to find the corresponding class file and import;
- Validation: Check the correctness of the loaded class file;
- Preparation: allocate memory space for static variables in a class;
- Resolution: The process by which the virtual machine replaces symbolic references in a constant pool with direct references. A symbolic reference is understood as an identifier, whereas a direct reference points directly to an address in memory.
- Initialization: Performs initialization on static variables and static code blocks.
What is the parental delegation model?
- Class loaders before introducing the parental delegation model. For any class, uniqueness within the JVM needs to be established both by the classloader that loads it and by the class itself. Each classloader has a separate class namespace. The classloader simply loads a class file into JVM memory with the specified fully qualified name and then converts it into a class object.
-
Class loader classification:
- Bootstrap ClassLoader, which is part of the VM itself, loads libraries in the Java_HOME/lib/ directory or in the path specified by the -xbootclasspath parameter and recognized by the VM.
- Other class loaders:
- The Extension ClassLoader is responsible for loading all libraries in the \lib\ext directory or the path specified by the java.ext.dirs system variable.
- The Application ClassLoader. The class loader is responsible for loading specified libraries on the user’s classpath, and we can use this class loader directly. In general, if we don’t have a custom class loader this is the default.
-
Parental delegation model: If a class loader receives a request for a class load, it first does not load the class itself. Instead, it delegates the request to the parent class loader, at each level, so that all load requests are sent to the top level of the starting class loader. The child loader attempts to load the class only if the parent load cannot complete the load request (it did not find the required class in its search scope).
-
The conclusion is: when a class receives a class loading request, it does not load the class itself first, but delegates it to the parent class, and the parent class loads it. If the parent class cannot load at this time, feedback to the child class, and the child class completes the class loading.
The JVM tuning
JVM tuning parameters can be set there
-
You can set it in the IDEA, Eclipse tools
-
If the WAR package is online, it can be set in Tomcat
-
If the Jar package is directly: java-jar is directly inserted into the JVM command
java -Xms1024m -Xmx1024m ... Wait for JVM arguments -jar springboot_app.jar &Copy the code
What about JVM tuning tools?
-
The JDK comes with many monitoring tools, which are located in the BIN directory of the JDK. The two most commonly used view monitoring tools are JConsole and JVisualVM.
-
Jconsole: For monitoring memory, threads, classes, etc in the JVM
-
Jvisualvm: The JDK comes with a versatile analysis tool, can analyze: memory snapshot, thread snapshot, program deadlock, monitoring memory changes, GC changes, etc.
-
What are the common JVM tuning parameters?
# Common Settings-xms: the initial heap size. The heap size is given when the JVM starts. -Xmx: indicates the maximum heap size that can be expanded by the JVM if the initial heap space is insufficient. -Xmn: Sets the size of the young generation in the heap. Total heap size = young generation size + Old generation size + persistent generation size. -xx :NewSize=n Sets the initial size of the young generation. -xx :MaxNewSize=n Sets the maximum size of the young generation. -xx :NewRatio=n Sets the ratio between the young generation and the old generation. For example, -xx :NewRatio=3, indicating that the ratio of young generation to old generation is 1:3, and young generation accounts for 1/4 of the sum of young generation + old generation. -xx :SurvivorRatio=n Ratio of Eden zone and two Survivor zones in young generation. Notice that there are two Survivor zones. 8 means two Survivor: Eden =2:8, i.e. one Survivor is 1/10 of the young generation. The default is 8-xss: sets the stack size for each thread. After JDK5, the Java stack size of each thread is 1M, whereas before, the stack size of each thread is 256K. -xx :ThreadStackSize=n ThreadStackSize -xx :PermSize=n sets the initial value of persistent generation -xx :MaxPermSize=n sets the size of persistent generation -xx :MaxTenuringThreshold=n sets the maximum age of young garbage objects. If set to 0, the young generation object passes through the Survivor zone and goes directly to the old generation.# Here are some less commonly used ones-xx :LargePageSizeInBytes=n Sets the memory page size of the heap memory. -xx :+UseFastAccessorMethods Optimizes the performance of getter methods of primitive types. -xx :+DisableExplicitGC Does not explicitly call System.gc() during run time, with -xx :+AggressiveOpts enabled by default whether to enable the latest tuning efforts of the JVM development team. For example, compiler optimization, biased locking, parallel generation collection, etc., jdk6 paper after the default start -xx :+UseBiasedLocking whether to enable biased locking, JDK6 default enabled -xnoclassGC whether garbage collection is disabled -xx :+UseThreadPriorities Use local thread priorities, default enabled etc......Copy the code
The JVM’s GC collector Settings
- -xx:+Use xxx GC
- XXX stands for garbage collector name
-xx :+UseSerialGC: sets the serial collector with the young collector. -xx :+UseParNewGC: sets the young user to collect in parallel. Can be used in conjunction with CMS collection. Above JDK5.0, the JVM will set itself based on the system configuration, so this value is no longer required. -xx :+UseParallelOldGC: -xx :+UseParallelOldGC: -xx :+UseParallelOldGC: -xx :+UseParallelOldGC: -xx :+UseConcMarkSweepGC: sets the concurrent collector for the old generation. -xx :+UseG1GC: sets the G1 collector, JDK1.9 default garbage collectorCopy the code
Finally, if this article can help you, I hope you can like forwarding plus a concern, support xiaobian!
The JVM, locks, high concurrency, reflection, Spring principles, microservices, Zookeeper, databases, data structures, and more.
Background private letter reply [information] you can get information for free!