JVM explores Native

Interview questions that will be asked in the Java Virtual Machine (jvm) interview.

  1. Could you please talk about your understanding of JVM? Java8 virtual machine and previous changes update?
    A feature of the java language is platform independence (a project can be executed on multiple operating systems), and jVM is the key to achieving this feature.
    Because the JVM has built another operating system on top of the operating system.
  2. What is OOM, and what is StackOverFlowError? How to analyze it?
  3. What are the common tuning parameters of JVM?
  4. How to capture memory snapshots and how to analyze dump files?
  5. Talk about your understanding of the class loader in the JVM?

JVM knowledge points

  1. Location of JVM
    Insert picture description here

  2. JVM architecture
    Insert picture description here
    Insert picture description here

  3. Class loader
    Function: Load Class file
    Start class loader: Load the jar package since JVM is running
    Extended class loader: Load the jar package in ext in the lib directory of jdk=>jvm
    Application class loader: Specify the path according to ClassPath , Find all the loaded class bytecode files of the application, load the class object corresponding to the type from the bytecode file, the
    class starts from being loaded into the virtual machine memory, until it is unloaded out of the memory, its life cycle:
    loading, verification , Preparation, Analysis, Initialization, Use, Unloading
    Among them, only the order of the five stages of loading, verification, preparation, initialization, and unloading is determined.
    Detailed description: link

  4. Parental delegation mechanism
    Insert picture description here

  • Bootstrap classLoader: Mainly responsible for loading core class libraries (java.lang.* etc.), constructing ExtClassLoader and APPClassLoader.
  • ExtClassLoader: Mainly responsible for loading some extended jars in the jre/lib/ext directory.
  • AppClassLoader: Mainly responsible for loading the main function class of the application.
    This design has the advantage that if someone wants to replace the system-level class: String.java. Its implementation is tampered with, but under this mechanism, the classes of these systems have been loaded by the Bootstrap classLoader, so they will not be loaded again, which prevents the implantation of dangerous codes to a certain extent.
  1. Sandbox
    Security Mechanism The core of the Java security model is the Java sandbox. What is a sandbox? A sandbox is an environment that restricts the running of programs. The sandbox mechanism is to limit the Java code to the specific operating range of the virtual machine (JVM), and strictly restrict the code's access to the local system resources, through such measures to ensure the effective isolation of the code and prevent damage to the local system. The sandbox mainly restricts access to system resources. What does the system resource include? -CPU, memory, file system, network. Different levels of sandboxes have different restrictions on access to these resources.
    Detailed description:
  2. Native

Insert picture description here
Native is a computer function, and a Native Method is an interface for Java to call non-Java code. The method is implemented in a non-Java language, such as C or C++.
Native: Anything that represents the native keyword means that the scope of Java is not up to the reach. Go back and call the ku of the underlying C language!
Will enter the local method stack
Call the local interface JNI
JNI role: expand the use of Java, fusion of different programming languages ​​for Java use! Initially: C, C++.
When Java was born, C and C++ were rampant. If you want to get a foothold, you must have C and C++ programs.
It opens up a special mark area in the memory area: Native Method Stack, which registers the native method.
In the final execution, the method in the local method library is loaded and used through JNI
: the Java program drives the printer and the management system will use it. It is relatively rare in enterprise-level applications.

  1. PC register
    Program counter: Program Counter Register
    Each thread has a program counter, which is private to the thread. It is a pointer that points to the method bytecode in the method area (used to store the address that points to an instruction that is about to be executed The instruction code), read the next instruction in the execution engine, is a very small memory space, almost negligible
  2. Method Area
    Method Area Method Area Method Area
    is shared by all threads, all fields and method bytecodes, as well as some special methods, such as constructors, interface codes are also defined here, in short, all the defined method information is stored in This area, this area belongs to the shared area;
    static variables, constants, class information (construction methods, interface definitions), and runtime constant pool are all stored in the method area, but the instance variables are stored in the heap memory and have nothing to do with the method area.
    static final Class, constant pool
  3. Stack
    Stack: data structure
    Program = data structure + algorithm continuous learning ~
    program = framework + business logic: eating ~
    stack: first in last out, last in first out
    queue: first in first out (FIFO: First Input First Output)
    method of program execution , Must be at the top of the stack.
    If you drink too much, vomiting is a stack, if you eat too much, pulling is a queue

Stack: There are also other names, stack memory, in charge of the operation of the program, life cycle and thread synchronization; when the
thread ends, the stack memory is also released. For the stack, there is no garbage collection problem.
Once the thread ends, the stack will Over!

Will put in the stack: 8 basic types + object reference + instance method

Stack operation principle: Stack frame When the
stack is full, an error will be reported: StackOverflowError

Stack + heap + method area: interactive relationship
Insert picture description here

  1. Three kinds of JVM
  • Sun公司 HotSpot Java HotSpot™ 64-Bit Server VM (build 25.251-b08, mixed mode)
  • BEA Jrockit
  • IBM J9 VM

I usually use HotSpot

Insert picture description here

  1. Heap
    Heap, only a single JVM heap memory, heap memory size is adjustable.
    After the class loader reads the class file, what will it generally put in the heap?
    Classes, methods, constants, variables, real objects of all reference types;

The heap memory is subdivided into three areas:

  • New Area (Eden Park) Young/New
  • Retirement area old
  • Perm

GC garbage collection is mainly in the Eden Park and the elderly care area~ When the
memory is full, it will prompt the OOM error report. Not enough heap memory! java.lang
.OutOfMemoryError: Java heap space
has changed its name (meta space) after jdk1.8.
12. Newborn area, senior area
Newborn area:

  • Class: place of birth and growth, even death
  • Garden of Eden, all the objects are new in the Garden of Eden!
  • Survivor area (0, 1)
    elderly area:
  • The new area is left, it will be here.
    Insert picture description here
    Truth: After research, 99% of objects are temporary objects!
  1. Permanent area
    Permanent area:
    This area is resident in memory. Used to store Class objects carried by the JDK itself. Interface metadata stores some environment or class information during Java runtime. There is no garbage collection in this area! Closing the VM virtual machine will release the memory in this area.

Under what circumstances, the permanent area will collapse:
A startup class loads a large number of third-party jar packages. Tomcat has deployed too many applications, a large number of dynamically generated reflection classes. Constantly being loaded. Until the inside is full, OOM will appear;

  • Before jdk1.6: permanent generation, the constant pool is in the method area
  • jdk1.7: Permanent generation, but slowly degraded, to permanent generation, the constant pool is in the heap
  • After jdk1.8: permanent generation, the constant pool is in the meta space

Insert picture description here
Metaspace: logically exists: physically does not exist
In a project, an OOM failure occurs suddenly, how to troubleshoot:

  • Can see the error in the first few lines of the code: memory snapshot analysis tool, MAT, Jprofiler
  • Dubug, analyze the code line by line!
    MAT, Jprofiler role
  • Analyze Dump memory files to quickly locate memory leaks;
  • Get the data in the heap

  • profiler
  1. Heap memory tuning
    GC: When the garbage collection
    Insert picture description here
    JVM performs GC, the three areas of the heap are not collected in a unified manner. Most of the time, recycling is the new generation
  • Cenozoic
  • Survival area

  • There are two types of GC in the elderly : light GC (normal GC) and heavy GC (global GC)
  1. Common algorithms:

GC topic:

  • JVM memory model and partition ~ detailed to each partition?
  • What are the partitions in the heap? Eden, form, to, elderly area, talk about their characteristics!
  • What are the GC algorithms? How to use the mark removal method, mark compression, copy algorithm, reference counter?
  • When do light GC and heavy GC occur respectively?

Reference counting method:
Insert picture description here
Copy algorithm:
Insert picture description here

Every GC will move Eden's live objects to the survivor area. Once the newborn area is GC, it will be empty.

Survival area, empty is to

When an object has undergone 15 GCs, it has not yet died
-XX: -XX:MaxTenuringThreshold=5
This parameter can be used to set the time to enter the retirement area

Insert picture description here

  • Benefit: no memory fragmentation
  • Disadvantages: waste of memory space, half of the space is always empty to. Assuming 100% survival of the subject (extreme situation)

The best use scenario for the replication algorithm: When the object's survival rate is low: Newborn area~

Mark removal algorithm:
Insert picture description here

  • Advantage: No extra space required!
  • Disadvantages: Two scans are a serious waste of time and memory fragmentation will occur.

Mark removal compression:
Insert picture description here
16. Summary
Memory efficiency: Copy algorithm>Mark removal algorithm>Mark compression algorithm (time complexity)
Memory neatness: Copy algorithm=Mark compression algorithm>Mark removal algorithm
Memory utilization: Mark compression algorithm=Mark removal algorithm >Copy Algorithm

Consider a question: Is there no optimal algorithm?
There is no best algorithm, only the most suitable algorithm. ------->GC: Analysis and collection algorithm

Young generation:

  • Low survival rate
  • Copy algorithm

Old age:

  • Large area: high survival rate
  • Mixed implementation of mark removal + mark compression

Learning JVM in a short period of time is unrealistic. If you want to study it in depth, you must go down and spend time, look at the interview questions, and "In-depth understanding of JVM".
However,

Guess you like

Origin blog.csdn.net/yang13676084606/article/details/110082785
JVM
JVM