What to do if the machining center is locked

What should I do if the machining center is locked? The two subsystems are Class loader and Execution engine; the
two components are Runtime data area and Native Interface.
Class loader (class loading): according to the given fully qualified class name (such as: java.lang.Object) to load the class file to the method area in the Runtime data area.
Execution engine (execution engine): execute instructions in classes.
Native Interface (native interface): interact with native libraries, is an interface for interaction with other programming languages.
Runtime data area (runtime data area): This is what we often say the JVM memory.
Process: First, the Java code is converted into byte code by the compiler, and then the class loader (ClassLoader) loads the byte code into the memory and places it in the method area of ​​the runtime data area, and The bytecode file is just a set of instruction set specifications of the JVM, and cannot be directly handed over to the underlying operating system for execution. Therefore, a specific command parser execution engine (Execution Engine) is required to translate the bytecode into the underlying system instructions and then submit It is executed by the CPU, and in this process, the native library interface (Native Interface) of other languages ​​needs to be called to realize the function of the entire program.

Speaking of the JVM runtime data area, the
Java virtual machine divides the memory area it manages into several different data areas during the execution of the Java program. These areas have their own purposes, as well as the time of creation and destruction. Some areas exist with the start of the virtual machine process, and some areas are established and destroyed depending on the start and end of the thread. The memory managed by the Java virtual machine is divided into the following areas:

Simply put, it is what we java runtime is placed there
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Program Counter Register: The line number indicator of the bytecode executed by the current thread. The job of the bytecode parser is to change the value of this counter to select the next bytecode instruction to be executed, branch , Loop, jump, exception handling, thread recovery and other basic functions, all need to rely on this counter to complete;

Why do we need a thread counter? Because the thread does not have the memory function

Java Virtual Machine Stack (Java Virtual Machine Stacks): Each method will create a stack frame (Stack Frame) in the Java virtual machine stack for storing local variable table, operand stack, dynamic link, method exit, etc. information;

The stack frame is the next unit in the Java virtual machine stack

Native Method Stack (Native Method Stack): The function is the same as the virtual machine stack, except that the virtual machine stack serves Java methods, and the local method stack serves the virtual machine to call Native methods;

Native keyword modification method is invisible, most of the source code of Native method is C and C ++ code

Java Heap: The largest piece of memory in the Java virtual machine is shared by all threads. Almost all object instances are allocated memory here;

Method area (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.

There is a detailed description of the JVM runtime data area later

Detailed introduction of the program counter? (Important understanding) The
program counter is a small memory space, it can be seen as: save the address (line number) of the bytecode instruction being executed by the current thread

Since the multithreading of the Java virtual machine is implemented by thread switching and allocating processor execution time, a processor can only execute instructions in one thread. Therefore, in order to restore the correct execution position after thread switching, each thread has an independent program counter, and the counters of each thread do not affect each other and are stored independently. This is called "thread private" memory. The program counter memory area is the only area in the virtual machine that does not specify OutOfMemoryError.

Summary: It can also be called a thread counter

Example: In Java, the smallest execution unit is a thread. A thread is to execute an instruction. The final operation of the executed instruction is our computer, which is the CPU. There is a very unstable factor when running on the CPU. It is called a scheduling strategy. This scheduling strategy is based on time slices, that is, the current nanosecond is allocated to that instruction.

if:

A thread watching live
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Suddenly, the thread B to a video phone, will snatch thread A time slice, it will break the thread A, thread A will hang
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Then, the video call ends. What should thread A do?
(Thread is the smallest execution unit, he does not have a memory function, he is only responsible for doing it, then this memory is recorded by: program counter)
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Introduce the Java virtual machine stack in detail? (Emphasis on understanding) The
Java virtual machine is private to the thread, and its life cycle is the same as the thread.

The virtual machine stack describes the memory model of Java method execution: each method will create a stack frame (Stack Frame) for storing local variable table, operand stack, dynamic link, method exit and other information during execution.

Explanation: There is a unit in the virtual machine stack, the unit is the stack frame, one method for each stack frame. In a stack frame, he has to store, local variables, operand stack, dynamic link, export, etc.

åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Parse the stack frame:

Local variable table: It is used to store our temporary 8 basic data types, object reference address, returnAddress type. (The returnAddress holds the instruction address of the bytecode to be executed after return.)

Operand stack: The operand stack is used to operate. For example, there is an i = 6 * 6 in the code. He will operate at the beginning, read our code, and then put it into the local variable table after calculation. Go

Dynamic link: If there is a service.add () method in my method, I want to link to another method. This is the dynamic link, where the link is stored.
Export: What is the export? If the export is normal, the return is abnormal. If the export is abnormal, an exception is thrown.

Will a method call another method create many stack frames?

Answer: Will be created. If there is a dynamic link in a stack to call another method, it will create a new stack frame. The stack is in order, one stack frame calls another stack frame, and the other stack frame will be arranged below the caller.

What does the stack point to the heap mean?

What does the stack point to the stack mean, what do you do if you want to use member variables in the stack? No member variables are stored in the stack, only an application address

Will recursive calls create many stack frames by themselves?

Answer: If recursive, multiple stack frames will also be created, that is, the stack will be lined up from top to bottom.

Can you give me a detailed introduction to the Java heap? (Emphasis on understanding) The
java heap (Java Heap) is the largest piece of memory managed by the java virtual machine, and it is a memory area shared by all threads. It is created when the virtual machine starts. . The sole purpose of this memory area is to store object instances.

The description in the Java Virtual Machine Specification is: All object instances and arrays must be allocated on the heap.

The java heap is the main area managed by the garbage collector, so it is also called the "GC heap".

From the perspective of memory recycling, the Java heap can be divided into: the new generation and the old generation.

From the perspective of memory allocation, multiple thread private allocation buffers may be divided in the Java heap shared by threads.

No matter how it is divided, it has nothing to do with the stored content. No matter which area is stored, it is an object instance. Further division is to better reclaim memory or allocate memory faster.
According to the Java Virtual Machine Specification, the Java heap can be in physically discontinuous memory space. The current mainstream virtual machines are all extensible (controlled by -Xmx and -Xms). If there is no memory in the heap to complete the instance allocation, and the heap can no longer expand, an OutOfMemoryError exception will be thrown.

Can you explain the local method stack?

The local method stack is easy to understand. He is very similar to the stack, except that the stack word with the native keyword on the method

It is the service method of the virtual machine stack that executes the Java method (that is, bytecode) for the virtual machine. The method of the
native keyword is not visible. You must go to the Oracle official website to download it to see it, and the native keyword is modified. Most of the source code is C and C ++ code.

Similarly, the C and C ++ codes are in the local method stack

Can you explain the method area (focus on understanding)

The method area is a memory area shared by all threads. It is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the Java virtual machine.

It has a special order called Non-Heap (non-heap). When the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception is thrown.

What is the JVM bytecode execution engine

The core component of the virtual machine is the execution engine, which is responsible for executing the bytecode of the virtual machine. Generally, the user first compiles it into machine code and executes it.

"Virtual machine" is a concept relative to "physical machine". The bytecode of a virtual machine cannot be run directly on the physical machine. It needs a JVM bytecode execution engine-it can be executed on the physical machine after being compiled into machine code.

Have you heard of direct memory?

Direct memory (Direct Memory) is not part of the virtual machine runtime data area, nor is it the memory area defined in the Java virtual machine. But this part of memory is also used frequently, and it may also cause OutOfMemoryError exception, so we will explain it here.

My understanding is that direct memory is an intermediate memory based on physical memory and Java virtual machine memory

Do you know a garbage collection system?

When the program is running, it will generate a lot of memory garbage (some memory objects pointed to by no reference are memory garbage, because these objects are no longer accessible, the program can't use them, and they are dead for the program) Runtime performance, java virtual machine continuously performs automatic garbage collection (GC) during the process of running the program.

The garbage collection system is the core of Java, and it is also indispensable. Java has its own mechanism for garbage collection, developers do not need to manually clean up

Part of the reason is that because of the powerful Java garbage collection system, Java leads the market

What is the difference between stacks?
image.png

note:

Static variables are placed in the method area

Static objects are still placed on the heap.

Deep copy and shallow copy
shallow copy (shallowCopy) just adds a pointer to the existing memory address.

Deep copy (deepCopy) is to add a pointer and apply for a new memory, so that the increased pointer points to the new memory,

Shallow copy: just point to the memory address to be copied. If the original address changes, the objects copied from the shallow copy will also change accordingly.

Deep copy: open up a new memory address in the computer to store the copied object.

Will there be a memory leak in Java? Please explain why?
Memory leak means that objects or variables that are no longer in use are always occupied in memory. In theory, Java has a GC garbage collection mechanism, that is, objects that are no longer used will be automatically collected by GC and automatically removed from memory.

However, even if this is the case, there is still a memory leak when the processing center is locked. The reason for the memory leak in java is very clear: long-period objects holding references to short-lived objects are likely to leak Although the short-lived object is no longer needed, it cannot be recycled because the long-lived object holds its reference. This is the occurrence of memory leaks in java.

Garbage collection mechanism and algorithm
Brief description of the Java garbage collection mechanism
In java, programmers do not need to explicitly release the memory of an object, but by the virtual machine itself. In the JVM, there is a garbage collection thread, which is of low priority and will not be executed under normal circumstances. Only when the virtual machine is idle or the current heap memory is insufficient, the execution will be triggered. Reference the objects and add them to the collection to be recycled for recycling.

What is GC? Why GC
GC means garbage collection (Gabage Collection), memory processing is a place where programmers are prone to problems. Forgetting or wrong memory collection will cause the program or system to become unstable or even crash. The GC function provided by Java can be automatically monitored Whether the object exceeds the scope to achieve the purpose of automatically reclaiming memory, the Java language does not provide a display operation method for releasing allocated memory.

The advantages and disadvantages
of garbage collection Advantages: The JVM's garbage collector does not require us to manually deal with unreferenced objects, this is the biggest advantage

Disadvantages: The programmer cannot call the garbage collector on an object or all objects in real time for garbage collection.

What is the principle of the garbage collector? Is there any way to perform garbage collection manually?
For GC, when a programmer creates an object, the GC begins to monitor the address, size, and usage of the object.

Generally, GC uses a directed graph to record and manage all objects in the heap. In this way, determine which objects are "reachable" and which objects are "unreachable." When the GC determines that some objects are "unreachable", the GC is responsible for reclaiming these memory spaces.

can. The programmer can manually execute System.gc () to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed.

What reference types are in the JVM?
Strong reference: It will not be recycled when gc occurs.
Soft reference: useful but not necessary objects, will be collected before a memory overflow occurs.
Weak references: useful but not necessary objects, will be recycled in the next GC.
Virtual reference (ghost reference / phantom reference): The object cannot be obtained through virtual reference. PhantomReference is used to implement virtual reference. The purpose of virtual reference is to return a notification when gc.

How to judge whether the object can be recycled?
When the garbage collector is doing garbage collection, the first thing to determine is which memory needs to be recycled, which objects are alive and cannot be recycled; which objects are dead and need to be recycled.

There are generally two ways to judge:

Reference counter method: Create a reference count for each object. When there is an object reference, the counter is +1. When the reference is released, the counter is -1. When the counter is 0, it can be recycled. It has a disadvantage that it cannot solve the problem of circular references; (this has been eliminated)

Reachability analysis algorithm: search downwards from GC Roots, and the path that the search takes is called the reference chain. When an object is connected to GC Roots without any reference chain, it proves that the object can be recycled. (Very, very widely used in the market)

What is Full GC?
Clean up the entire heap space-including young and old generations and permanent generations.
Because Full GC is to clean up the entire heap space, the execution speed of Full GC is very slow. In Java development, it is best to ensure that Full GC is not triggered.


When an object can be collected by the garbage collector When the object becomes inaccessible to the application that currently uses the object, the object can be collected.

Garbage collection does not occur in the permanent generation. If the permanent generation is full or exceeds the threshold, it will trigger a full garbage collection (Full GC). If you look closely at the output of the garbage collector, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is very important to avoid Full GC.

What are the JVM garbage collection algorithms?
Mark-sweep algorithm: mark useless objects, and then clear and recycle. Disadvantages: The efficiency is not high, and garbage debris cannot be removed.

Copy algorithm: divide two memory areas of equal size according to the capacity, when one block is used up, copy the live objects to another block, and then clean up the used memory space once Disadvantages: memory usage is not high, only half of the original.

Marking-organizing algorithm: mark useless objects, move all surviving objects to one end, and then directly clear the memory beyond the end boundary.

Generational algorithm: The memory is divided into several blocks according to the different life cycle of the object, generally the new generation and the old generation. The new generation basically adopts the replication algorithm, and the old generation adopts the mark arrangement algorithm.

Mark-sweep algorithm
Marks useless objects and then clears and recycles them.

Mark-sweep algorithm (Mark-Sweep) is a common basic garbage collection algorithm, which divides garbage collection into two stages:

Marking stage: mark the objects that can be recycled.

Clearing phase: Reclaim the space occupied by the marked objects.

The mark-sweep algorithm is basic because the garbage collection algorithms described later are improved on the basis of this algorithm.

Advantages: simple implementation, no need to move objects.

Disadvantages: The efficiency of the marking and cleaning process is low, a large number of discontinuous memory fragments are generated, and the frequency of garbage collection is increased.

Mark - sweep algorithm performs a process as shown
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Copy algorithm
In order to solve the problem of low efficiency of the mark-and-sweep algorithm, a copy algorithm was created. It divides the memory space into two equal areas, using only one area at a time. During garbage collection, traverse the currently used area, copy the surviving objects to another area, and finally collect the recyclable objects of the currently used area.

Advantages: Allocate memory in sequence, simple to implement, efficient to run, without considering memory fragmentation.

Disadvantages: The available memory size is reduced to half of the original, and the object is frequently copied when the survival rate is high.

Copy algorithm execution as shown below
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Mark-and-sort algorithm The
replication algorithm can be used in the new generation, but the replication algorithm cannot be selected in the old generation, because the survival rate of objects in the old generation will be higher, so there will be more copy operations, resulting in lower efficiency.

The mark-sweep algorithm can be applied in the old generation, but it is not efficient, and it is easy to generate a large amount of memory fragments after the memory is reclaimed. Therefore, there is a Mark-Compact algorithm, which is different from the Mark-Compact algorithm. After marking the recyclable objects, all the surviving objects are compressed to one end of the memory, so that they are arranged compactly. Together, and then reclaim the memory beyond the end boundary. After recycling, the used and unused memory are on each side.

Advantages: Solved the problem of memory fragmentation in the mark-clean algorithm.

Disadvantages: The local object still needs to be moved, which reduces the efficiency to a certain extent.

Mark - finishing execution of the algorithm as shown in FIG
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Generational collection algorithm The
current commercial virtual machines all use generational collection of garbage collection algorithms. Generational collection algorithm, as the name implies, divides the memory into several blocks according to the life cycle of the object. Generally including the young generation, years old and permanent generations, as shown in Figure :( focused explain later)
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

Will garbage collection occur in the permanent generation in the JVM?
Garbage collection will not occur in the permanent generation. If the permanent generation is full or exceeds the threshold, a full garbage collection (Full GC) will be triggered. If you look closely at the output of the garbage collector, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is very important to avoid Full GC. Please refer to Java8: From the permanent generation to the metadata area
(Note: The permanent generation has been removed in Java8, and a new native memory area called the metadata area has been added)

Garbage collector and the new generation, old time, permanent generation of
talk about the new generation, old time, permanent generation difference
åœ¨è¿ ™ é ‡ Œæ ?? 'å
¥ å ›¾ç ‰ ‡ æ ???? è¿ °

In Java, the heap is divided into two different areas: the young generation (Old) and the old generation (Old). The young generation (Young) is divided into three areas: Eden, From Survivor, To Survivor. The purpose of this division is to enable the JVM to better manage objects in the heap memory, including memory allocation and recycling.

The new generation generally saves newly emerged objects, so a large number of objects are found to die every time during garbage collection. Only a small number of objects survive. The replication algorithm is adopted, and only a small cost of replication of the surviving objects is needed to complete the collection.

In the old generation, objects that have survived for a long time are generally preserved. They have a high survival rate and no additional space to guarantee it. Therefore, they must use the "mark-clean" or "mark-sort" algorithm.

The permanent generation is the method area of ​​the JVM. Here are all some class information, static variables, constants and other data loaded by the virtual machine. Things in this area are more difficult to recycle than the old and new generations.

What is Minor GC, Major GC, Full GC?
Minor GC is a new generation GC, referring to the garbage collection action that occurs in the new generation. Since Java objects are mostly dead, the Minor GC is very frequent, and the recovery speed is generally faster. (Generally use copy algorithm to recycle garbage)

Major GC is an old generation GC. It refers to a GC that occurred in the old generation. Usually, Major GC will be executed in conjunction with Minor GC. Major GC is much slower than Minor GC. (Clear marking method and marking finishing method can be used)

Full GC is to clean up the entire heap space, including the young and old generations

Minor GC, Major GC, Full GC differences and trigger conditions
Minor GC trigger conditions are generally:

When the eden area is full, MinorGC is triggered. That is, when applying for an object, it is found that the eden area is not enough, and a MinorGC is triggered.

Minor GC is triggered when the size of the newly created object> Eden remaining space

The trigger conditions for Major GC and Full GC are generally:

Major GC is usually equivalent to full GC

Every time the processing center is locked, what should I do? The average size of the objects promoted to the old generation> The remaining space
in the old
generation . The objects that survive after MinorGC exceed the remaining space in the old generation. The permanent generation is insufficient. The
implementation of System.gc ()
CMS GC abnormal
heap memory allocation is very Big object

Published 28 original articles · Likes0 · Visits 907

Guess you like

Origin blog.csdn.net/srhgsr/article/details/105545610