Persevere because of love, and wait because of love. After a long period of no show to play, the spring of life finally came. He is gradually known and loved by others
Three years ago, when I was asked by the interviewer in Suzhou Park, it was the JVM question. The interview questions repeatedly summarized according to the knowledge points need to be updated.
- ①. What is included in the JDK?
- ②. Three major commercial virtual machines
- ③. Talk about your overall understanding of JVM
- ④. Briefly describe the Java class loading mechanism (Java class loading process)
- ⑤. What is class loading, linking, and initialization
- ⑥. Active and passive use of classes
- ⑦. What is the difference between forName("Java.lang.String") and loadClass("Java.lang.String")?
- ⑧. It is relatively simple to determine whether a constant is "deprecated", and the condition to determine whether a type belongs to a "class no longer used" is more demanding. Need to meet the following three conditions at the same time
- ⑨. Talk about the class loader classification?
- ⑩. Tell me about your understanding of boot class loader, extended class loader and application class loader
- ⑩①. Talk about your understanding of the parental delegation mechanism
- ⑩②. Advantages of the parental delegation mechanism
- ⑩③. Disadvantages of the parental delegation model
- ⑩④. Can the parental delegation mechanism be broken? why
- ⑩⑤. Talk about your understanding of sandbox security mechanism
- ⑩⑥. Talk about your understanding of loadClass in ClassLaoder
- ⑩⑦. The storage location of the basic data types in Java in the JVM
- ⑩⑧. What is in the bytecode file?
- ⑩⑨. What is in the bytecode file?
- ②0. Will garbage collection occur in the permanent generation of JVM?
- ②①. When JVM loads classes, is it executed in the order of class loading, linking (verification, preparation, analysis) and initialization?
- ②②. Is the relationship between "parent loader" and "child loader" inherited?
- ②③. If we customize the loader without using the parent delegation mechanism, will the core class library in Java still be loaded?
- ②④. Why does JDBC need to break the parent delegation mechanism? (Tomcat also broke)
- ②⑤. Since Tomcat does not follow the parental delegation mechanism, if I define a malicious HashMap myself, will it be risky?
- ②⑥. Let's think about it: Tomcat is a web container, so what problem does it solve? (1). A web container may need to deploy two applications. Different applications may rely on different versions of the same third-party library. The class libraries of each application are independent to ensure mutual isolation.
- ②⑦. What should I do if CommonClassLoader of tomcat wants to load classes in WebAppClassLoader?
- ②⑧. Why do java files placed in the src folder in Eclipse/IDEA will give priority to the class in the jar package
- ②⑨. Talk about your understanding of the program counter
- ③0. Why use the PC register to record the execution address of the current thread?
- ③①. Why is the PC register set as thread private?
- ③①. Tell me about your understanding of the local method stack
- ③①. What virtual machine stack?
- ③②. Does GC exist in the stack?
- ③③. The comparison between static variables and local variables?
- ③④. What is in the virtual machine stack?
- ③⑤. Tell me about your understanding of the local variable table?
- ③⑥. Tell me about your understanding of the operand stack?
- ③⑦. Tell me about your understanding of dynamic links?
- ③⑧. Method return address?
- ③⑨. Additional information (understand)
- ④0. Can the stack size be adjusted to ensure that there is no overflow?
- ④①. The larger the allocated stack memory, the better?
- ④②. Will garbage collection involve the virtual machine stack?
- ④③. Are the local variables defined in the method thread safe?
- ④④. Under what circumstances will stack memory overflow occur?
- ④⑤. Tell me about the difference between heap and stack?
- ④⑥. Briefly talk about your understanding of the heap? (Share|Garbage Collection)
- ④⑦. Partitions in the heap: Eden, survival (from+to), old age, their own characteristics
- ④⑧. What parameters are used to set the heap size?
- ④⑨. The initial heap size is the same as the maximum heap size. What are the benefits of this?
- ⑤0. What are the allocation rules for objects in the heap?
- ⑤①. Minor GC | Major GC | Full GC
- ⑤②. When will minor gc be triggered?
- ⑤③. Are all objects allocated in the heap?
- ⑤④. What are stored in the method area?
- ⑤⑤. The size of the method area determines how many classes the system can save. If the system defines too many classes, the method area will overflow, and the virtual machine will also throw a memory overflow error?
- ⑤⑥. Talk about your changes to the string constant pool and static variables in the method area?
- ⑤⑦. Why use metaspace to replace permanent generation
- ⑤⑧. Why should StringTable be adjusted?
①. What is included in the JDK?
①. JDK is provided to Java developers to use, which includes java development tools, and also includes JRE. So after installing JDK, there is no need to install JRE separately. Among the development tools: compilation tool (javac.exe) packaging tool (jar.exe), etc.
②. JRE (Java Runtime Environment): Including Java Virtual Machine (JVM Java Virtual Machine) and core class libraries required by Java programs, etc. If you want to run a developed Java program, you only need to install JRE in your computer Can
②. Three major commercial virtual machines
①. Sun's HotSpot VM (later acquired by Oracle)
②. BEA JRocket VM (later acquired by Oracle), there is no interpreter in the JRocket virtual machine, and all codes are compiled and executed by the just-in-time compiler
③. iBM J9
③. Talk about your overall understanding of JVM
①. Class loader subsystem
②. Runtime data area [Our core focus is on the stack, heap, and method areas here]
③. Execution engine (interpreter and JIT compiler coexist)
④. Briefly describe the Java class loading mechanism (Java class loading process)
- The loading mechanism refers to the process of loading, linking, and initializing classes
⑤. What is class loading, linking, and initialization
①. Load: Load the .class file in the bytecode file into the method area of the runtime data area through the class loader, and create a large Class object
②. Link: (Verification, Preparation, Analysis)
- Verification (for example, verify that the bytecode file starts with CAFFBABA, version number, etc.)
- Preparation (assign the default initialization value to the class variable, use static+final modification, and display assignment does not involve the basic data type of the method or constructor call or the display assignment of the String type are in the preparation stage)
- Resolution: Turn symbol references in the class into direct references (symbol quotes are in the constant pool of the bytecode file)
- ③. Initialization: Assign the correct initialization value to the class variable, and execute the Clinit method (static code block or variable modified with static).
Note: The class variable is declared in a class, but the initialization statement of the class variable and the static code block are not used explicitly. When performing an initialization operation
⑥. Active and passive use of classes
①. When creating an instance of a class, such as using the new keyword, or through reflection, cloning, or deserialization
②. Access a static variable of a certain class or interface, or assign a value to the static variable
③. Call the static method of the class
④. Reflection (for example: Class.forName("com.xiaozhi.Test"))
⑤. Initialize a subclass (when initializing the subclass, if you find that the parent class has not been initialized, you need to trigger the initialization of the parent class first)
⑥. When the virtual machine starts, the user needs to specify a main class to be executed (the class containing the main() method), and the virtual machine first initializes the main class
⑦. The dynamic language support provided by JDK7 (involving parsing the corresponding classes of REF_getStatic, REF_putStatic, and REF_invokeSt atic method handles)
⑦. What is the difference between forName("Java.lang.String") and loadClass("Java.lang.String")?
①. forName() will cause the active loading of the class, and getClassLoader() will not cause the active loading of the class, Class.forName(): is a static method, the most commonly used is Class.forName(String className); according to the incoming The fully qualified name of the class returns a Class object. This method will perform class initialization while loading the Class file into memory
②. ClassLoader.loadClass(): This is an instance method and requires a ClassLoader object to call this method. When this method loads the Class file into the memory, the initialization of the class will not be performed, and the initialization will not be performed until the class is used for the first time. (This method needs to get a ClassLoader object, so you can specify which class loader to use as needed)
⑧. It is relatively simple to determine whether a constant is "deprecated", and the condition to determine whether a type belongs to a "class no longer used" is more demanding. Need to meet the following three conditions at the same time
①. All instances of this class have been recycled. That is, there is no instance of this class and any derived subclasses in the Java heap
②. The class loader that loaded this class has been recycled. Unless this condition is a carefully designed alternative class loader scenario. Such as JSP reloading and so on. Otherwise it is usually difficult to achieve
③. The Java.lang.Class object corresponding to this class is not referenced anywhere, and the method of this class cannot be accessed through reflection anywhere.
⑨. Talk about the class loader classification?
①. JVM supports two types of class loaders, namely Bootstrap ClassLoader and User-Defined ClassLoader
②. Conceptually, a custom class loader generally refers to a class loader customized by the developer in the program, but the Java virtual machine specification does not define this, but all derived from the abstract class ClassLoader Class loaders are divided into custom class loaders
⑩. Tell me about your understanding of boot class loader, extended class loader and application class loader
- ①. Boot class loader
- This class loading is implemented in C/C++ language and is nested inside the JVM
- It is used to load Java's core class libraries (JAVA_HOME/jre/lib/rt.jar, resource.jar or sum.boot.class.path path), used to provide the classes needed by the JVM itself (the String class is used Of this class loader)
- Due to security considerations, Bootstrap startup class loader only loads classes whose package names start with java, javax, sun, etc.
- Does not inherit from java.lang.ClassLoader, no parent loader
- ②. Extension class loader Extension
- Written in Java language and implemented by sum.music.Launcher$ExtClassLoader
- Derived from the ClassLoader class, the parent class loader is the startup class loader
- Load the class library from the directory specified by the java.ext.dirs system property, or load the class library from the jre/lib/ext subdirectory (extension directory) of the JDK installation directory. If the JAR created by the user is placed in this directory, it will also be automatically loaded by the extension class loader
- ③. System class loader (Application)
- Written in Java language and implemented by sum.music.Launcher$AppClassLoader
- Derived from the ClassLoader class, the parent class loader is the extended class loader
- It is responsible for loading the class library under the path specified by the environment variable classpath or the system property java.class.path
- Call System.getSystemClassLoader() | Thread.currentThread().getContextClassLoader() to get the system class loader
⑩①. Talk about your understanding of the parental delegation mechanism
①. If a class load receives a class load request, it will not load it first, but delegate the request to the parent class loader to execute
②. If the parent class loader still has its parent class loader, it will further delegate upwards, recursively, and the request will eventually reach the top-level startup class loader
③. If the loader of the parent class can complete the loading task of the class, it will return successfully. If the parent class loader cannot complete the loading task, the child loader will try to load by itself. This is the parent delegation mode
⑩②. Advantages of the parental delegation mechanism
①. Avoid repeated loading of classes and ensure the global uniqueness of a class (when the parent ClassLoader has already loaded the class, there is no need for the child ClassLoader to load it again) For
example: if we boot the class loader to load, there will be no It is necessary to use the extended class loader to load again
②. Protect the security of the program and prevent the core API from being tampered with at will
⑩③. Disadvantages of the parental delegation model
- The delegation process of checking whether the class is loaded is one-way. Although this method is structurally clear and makes the responsibilities of each ClassLoader very clear, it also brings a problem, that is, the top-level ClassLoader cannot access what is loaded by the bottom-level ClassLoader. class)
⑩④. Can the parental delegation mechanism be broken? why
①. The first "disruption" of the parental delegation model actually occurred before the emergence of the parental delegation model—that is, the "ancient" era before the advent of JDK 1.2
②. The second destruction of the parent delegation mechanism: thread context class loader (ClassLoader.getSystemClassLoader( ))
③. The third "destroy" of the parent delegation model is caused by the user's pursuit of program dynamics. Such as: code hot replacement (Hot Swap), module hot deployment (Hot Deployment)
⑩⑤. Talk about your understanding of sandbox security mechanism
- ①. Customize the String class, but when loading the String class, the boot class loader will be used to load it, and the boot class loader will first load the file that comes with the jdk (java\lang in the rt.jar package). \String.class), the error message says that there is no main method because the String class in the rt.jar package is loaded. This can ensure the protection of the java core source code, which is the sandbox security mechanism can protect the program security to a certain extent, and protect the native JDK code.
⑩⑥. Talk about your understanding of loadClass in ClassLaoder
①. First look up whether there is a target class in the cache of the current loader, if there is, return directly.
②. Determine whether the parent loader of the current loader is empty, if it is not empty, call the parent.loadClass(name, false) interface to load
③. Conversely, if the parent class loader of the current loader is empty, call the findBootstrapClassOrNull(name) interface to let the bootstrap class loader load
④. If it fails to load through the above 3 paths, call the findClass(name) interface to load. This interface will eventually call the native interface of the defineClass series of the java.lan g.ClassLoader interface to load the target Java class
⑩⑦. The storage location of the basic data types in Java in the JVM
- Local variables are stored in the stack, instance variables and static variables are stored in the heap
⑩⑧. What is in the bytecode file?
- ①. Magic, version (minor version, major version), constant pool (the cornerstone of bytecode files), access identification, class index | parent class index | interface index collection, field table collection, method table collection, attribute table collection
⑩⑨. What is in the bytecode file?
①. Literal and symbolic quotation marks
②. Literals: constants and strings in Java
③. Symbol quotes:
fully qualified names of classes and interfaces,
attribute names and modifiers,
method names and modifiers
②0. Will garbage collection occur in the permanent generation of JVM?
①. The garbage collection in the method area is mainly: abandoned constants and no longer used types
②. The HotSpot virtual machine's recycling strategy for the constant pool is very clear, as long as the constants in the constant pool are not referenced anywhere, they can be recycled
③. It is relatively simple to determine whether a constant is "abandoned", but the condition to determine whether a type belongs to a "class no longer used" is more demanding
. The following three conditions need to be met at the same time:
- All instances of this class have been recycled. That is, there are no instances of classes and any derived subclasses in the Java heap
- The loader that loaded the class has been recycled. This condition is usually difficult to achieve unless it is a designed alternative class loader scenario, reloading into OSGI, JSP, etc.
- The java.lang.Class object corresponding to this class is not referenced anywhere, and the method of this class cannot be accessed through reflection anywhere.
②①. When JVM loads classes, is it executed in the order of class loading, linking (verification, preparation, analysis) and initialization?
- However, the Java virtual machine specification does not explicitly require that the parsing phase must be executed in order. In the HotSpot VM, loading, verification, preparation, and initialization will be executed in an orderly manner, but the parsing operation in the link phase is often accompanied by the JVM after the initialization is executed.
②②. Is the relationship between "parent loader" and "child loader" inherited?
- Not an inherited relationship, but an inclusive relationship
②③. If we customize the loader without using the parent delegation mechanism, will the core class library in Java still be loaded?
- It will be loaded by the boot class loader, because whether it is a custom loader or a system class loader or an extended class loader, you must eventually call java.lang.ClassLoader.defineClass(String,byte,int,int , ProtectionDomain) method, and this method will execute the preDefineC lass() interface, which provides protection for the JDK core class library
②④. Why does JDBC need to break the parent delegation mechanism? (Tomcat also broke)
- The Driver interface of JDBC is defined in the JDK, and its implementation is provided by the service provider of each database, such as the MySQL driver package. The DriverManager class needs to load each class that implements the Driver interface, and then manage it, which means that the BootStrap class loader also To load the implementation class of the Driver interface in the jar package, this breaks the parent delegation mechanism
②⑤. Since Tomcat does not follow the parental delegation mechanism, if I define a malicious HashMap myself, will it be risky?
- Obviously there is no risk. If there is, Tomcat has been running for so many years, can it not be improved? Tomcat does not follow the parent delegation mechanism, but the custom classLoader order is different, but the top level is still the same, and you still have to go to the top level to request the classloader
②⑥. Let's think about it: Tomcat is a web container, so what problem does it solve? (1). A web container may need to deploy two applications. Different applications may rely on different versions of the same third-party library. The class libraries of each application are independent to ensure mutual isolation.
①. If you use the default class loader mechanism, it is impossible to load two different versions of the same class library. The default class adder is no matter what version you are, only your fully qualified class name, and only one Share
②. The same version of the same class library deployed in the same web container can be shared. Otherwise, if the server has 10 applications, then 10 copies of the same class library must be loaded into the virtual machine, which is nonsense.
The default class loader can be implemented because its responsibility is to ensure uniqueness.
③. The web container also has its own dependent class library, which should not be confused with the application class library. Based on security considerations, the class library of the container should be separated from the class library of the program. The
third problem is the same as the first problem.
④. The web container needs to support the modification of jsp. We know that the jsp file must be compiled into a class file before it can run in a virtual machine, but it is commonplace to modify the jsp after the program is running. Otherwise, what use do you want? Therefore, the web container needs to support the JSP modification without restarting.
We think about how we can implement the hot replacement of the JSP file. The JSP file is actually a class file. If it is modified, but the class name is still the same, the class loader will directly take the method area. The modified JSP will not be reloaded if it already exists in the. So what to do? We can directly unload the class loader of this jsp file, so you should have thought that each jsp file corresponds to a unique class loader. When a jsp file is modified, the jsp class loader is directly unloaded. Recreate the class loader, reload the jsp file
②⑦. What should I do if CommonClassLoader of tomcat wants to load classes in WebAppClassLoader?
- After reading the previous content about destroying the parent delegation model, we have a good idea. We can use the thread context class loader to achieve it. Using the thread context loader, the parent class loader can request the child class loader to complete the action of class loading.
②⑧. Why do java files placed in the src folder in Eclipse/IDEA will give priority to the class in the jar package
- The understanding of Tomcat's class loading mechanism is not difficult to understand. Because the files java in the src folder in Eclipse/IDEA and the JSP in webContent will be compiled into class files and placed in WEB-INF/class when tomcat is started.
The jar package referenced externally by Eclipse/IDEA is equivalent to being placed in WEB-INF/lib.
Therefore, classes compiled from java files or JSP files must be loaded first.
②⑨. Talk about your understanding of the program counter
①. Function: It is used to store the address pointing to the next instruction, which is also the instruction code to be executed. Read the next instruction by the execution engine
②. It is thread-private, and there will be no memory overflow (the only area where the runtime data area does not have OOM)
③. If the execution is a Native method, then this counter is undefined
③0. Why use the PC register to record the execution address of the current thread?
①. Because the CPU needs to switch each thread continuously, after switching back at this time, you have to know where to continue execution
②. The bytecode interpreter of the JVM needs to change the value of the PC register to clarify what bytecode instruction should be executed next
③①. Why is the PC register set as thread private?
- In order to accurately record the current bytecode instruction address being executed by each thread, the best way is naturally to allocate a PC register for each thread
③①. Tell me about your understanding of the local method stack
①. The function of the local interface is to integrate different programming languages for Java. Its original intention is to integrate C/C++ programs. When Java was born, when C/C++ was rampant, if you want to gain a foothold, you must call C/C++ programs. Therefore, a special area is opened in the memory to process the code marked as native. Its specific method is to register the native method in the Native Method Stack and load the native libraies when the Execution Engine is executed.
②. Examples of native methods: Unsafe classes such as clone wait notify hashCode in the Object class are all native methods
③①. What virtual machine stack?
- ①. Java Virtual Machine Stacks (Java Virtual Machine Stacks) threads are closely related, each time a thread is created, a Java stack is created correspondingly, so the Java stack is also a "thread-private" memory area, and this stack will contain more A stack frame, each time a method is called, a stack frame is created and pushed into the stack. The stack frame is a data structure used to store method data and some process results. The process of each method from the call to the final return result. It corresponds to the process of a stack frame from stacking to stacking [first in, last out]
③②. Does GC exist in the stack?
- There is no GC in the stack, there is OOM and StackOverflowError
③③. The comparison between static variables and local variables?
①. We know that there are two opportunities to initialize the class variable table. The first time is in the "preparation phase", system initialization is performed, and the class variable is set to zero. The other time is in the "initialization" phase, giving programmers The initial value defined in the code
②. Unlike the initialization of class variables, there is no initialization process in the local variable table, which means that once the local variable is defined, it must be considered to be initialized.
③④. What is in the virtual machine stack?
- Local variable table, operand stack, dynamic link, method return address, attachment information
③⑤. Tell me about your understanding of the local variable table?
①. Defined as a numeric array, mainly used to store method parameters and local variables defined in the method body. These data types include various basic data types, object references (reference) and return Address types)
②. Since local variables are built on the thread's stack and are thread private data, there is no data security problem
③. The required capacity of the local variable table is determined during compilation. (And saved in the maximum local variables data item of the method Code attribute, the size of the local variable table will not be changed during the running of the method)
④. Local variable table, the basic storage unit is slot (variable slot)
⑤. In the local variable table, the 32-bit type only occupies one slot (including the reference data type), the 64-bit type (long and double) occupies two slots, byte, short, char are converted to int before storage , boolean is also converted to int (0 means fasle, non-zero means true) long and double occupy two slots
⑥. Jvm will assign an access index to each slot in the local variable table, through this index you can successfully access the local variable value specified in the local variable table
⑦. If you need to access a 64-bit local variable value in the local variable table, you only need to use the previous index (for example: access long or double type variables)
⑧. If the current frame is created by the constructor or instance method, then the object reference this will be placed in the slot with index 0
⑨. The slots in the local variable table in the stack frame can be reused. If a local variable exceeds its scope, the new local variable applied after its scope is likely to reuse the expired local variable Slot, so as to save resources
⑩. The variables in the local variable table are also important garbage collection root nodes, as long as the objects directly or indirectly referenced in the local variable table will not be recycled
③⑥. Tell me about your understanding of the operand stack?
①. We say that the interpretation engine of the Java virtual machine is a stack-based execution engine, where the stack refers to the operand stack
②. In addition to the local variable table, each independent stack frame also contains a Last-In-First-Out (Last-In-First-Out) operand stack, which can also be called an expression stack
③. Each operand stack will have a clear stack depth for storing values. The maximum depth required is defined at compile time and stored in the Code attribute of the method, which is the value of max_stack
④. Any element in the stack can be any Java data type. The
32bit type occupies one stack unit depth and the
64bit type occupies two stack unit depths.
⑤. Operand stack, mainly used to save the intermediate results of the computer process, and as a temporary storage space for variables in the calculation process
③⑦. Tell me about your understanding of dynamic links?
- Dynamic linking is the process of turning symbol quotes in bytecode files (in the string constant pool) into direct references
③⑧. Method return address?
①. Store the value of the PC register that calls the method
②. There are two ways to end a method:
normal execution is completed,
unhandled exception occurs, abnormal exit
③. No matter which way to exit, after the method exits, it will return to the position where the method was called. When the method exits normally, the value of the caller's pc counter is used as the return address, that is, the address of the next instruction of the instruction that called the method.
However, if exiting through an exception, the return address is determined by the exception table, and this part of information is generally not saved in the stack frame.
③⑨. Additional information (understand)
- The stack frame is also allowed to carry some additional information related to the implementation of the Java virtual machine. For example, information that supports program debugging.
④0. Can the stack size be adjusted to ensure that there is no overflow?
- Can't. Because adjusting the stack size will only reduce the possibility of overflow, the stack size cannot be expanded indefinitely, so there is no guarantee that overflow will not occur
④①. The larger the allocated stack memory, the better?
- No, because increasing the stack size will cause the stack of each thread to become very large, making the number of threads that can be created under a certain stack space become smaller
④②. Will garbage collection involve the virtual machine stack?
①. No; garbage collection will only involve the method area and the heap, and the method area and the heap may also overflow
②. Program counter, only records the address of the next line to run, there is no overflow and garbage collection
③. Both the virtual machine stack and the local method stack only involve pushing and popping, there may be stack overflow, and there is no garbage collection
④③. Are the local variables defined in the method thread safe?
- If local variables are generated internally and die internally, it is thread-safe
④④. Under what circumstances will stack memory overflow occur?
①. The local array is too large. When the array inside the function is too large, it may cause stack overflow
②. There are too many levels of recursive calls. The recursive function will perform a stack operation when it is running. When the stack is pushed too many times, it will also cause a stack overflow.
④⑤. Tell me about the difference between heap and stack?
①. From the perspective of GC, OOM, StackOverflowError
[There is no GC in the stack, OOM will occur when a fixed-size stack, and StackOverflowError will occur dynamically. GC, OOM, StackOverflowError all exist in the heap]
②. Execution efficiency from the stack [The efficiency of the stack is higher than that of the heap]
③. Memory size, data structure
[The space of the heap is generally larger than that of the stack, and the stack is a FIFO first-in-last-out model. The structure in the heap is complex, there can be linked lists, arrays, etc.]
④. Stack tube operation, stack tube storage
④⑥. Briefly talk about your understanding of the heap? (Share|Garbage Collection)
①. The Java heap area is created when the JVM starts, and its space size is also determined. It is the largest piece of memory space managed by Jvm
②. All threads share the Java heap, where you can also divide the thread private buffer (Thread Local Allocation Buffer, TLAB)
③. After the method ends, the objects in the heap will not be removed immediately, only when garbage collected
(note: a process is a JVM instance, a process contains multiple threads)
④. All threads share the Java heap, where you can also divide the thread private buffer (Thread Local Allocation Buffer, TLAB)
④⑦. Partitions in the heap: Eden, survival (from+to), old age, their own characteristics
①. Divided into: new generation + old generation + permanent generation in JDK1.7 | Divided into: new generation + old generation + metaspace in JDK1.8
②. Cenozoic: Eden Park, survivors S0, S1 (8:1:1), almost all Java objects are new in the Eden area, and
most of the Java objects are destroyed in the Cenozoic. ; IBM’s special research shows that 80% of the objects in the new generation are the
old generations that " live and die" : the life cycle of another type of object is very long, and in some extreme cases, it can be compared with the life of the JVM. The cycle remains the same
New generation: old generation = 1:2
④⑧. What parameters are used to set the heap size?
①. -Xms: initial memory (default is 1/64 of physical memory)
②. -Xmx: Maximum memory (default is 1/4 of physical memory)
-XX:HandlePromotionFailure: space allocation guarantee -Xmn
: set the maximum memory size of the new generation, generally use the default value
④⑨. The initial heap size is the same as the maximum heap size. What are the benefits of this?
- Usually the two parameters -Xms and -Xmx are configured with the same value, the purpose is to be able to re-compute the size of the heap area after the java garbage collection mechanism cleans up the heap area, thereby improving performance
⑤0. What are the allocation rules for objects in the heap?
①. Priority allocation to Eden
②. Large objects are directly allocated to the elderly (try to avoid too many large objects in the program)
③. Long-term surviving objects are allocated to the old age
④. Dynamic object age judgment (if the total size of all objects of the same age in the Survivor area is greater than half of the Survivor space, objects with age greater than or equal to the age can directly enter the old age without waiting for the age required in MaxTenurningThreshold
⑤. Space allocation guarantee-XX:HandlePromotionFailure
(After JDK6 Update24, the rule becomes that only the continuous space of the old generation is larger than the total size of the new generation object or the average size of previous promotion will be Minor GC, otherwise it will be Full GC)
⑤①. Minor GC | Major GC | Full GC
①. Minor GC will be triggered when the Eden Eden Park is full, which occurs in the new generation
②. Major GC will be triggered when it is full in the old age. It occurs in the old age. The time of major gc is longer than that of minor gc.
③. Full GC occurs in the entire heap
⑤②. When will minor gc be triggered?
- Triggers when Eden Park is full
⑤③. Are all objects allocated in the heap?
- Almost all objects are allocated on the heap. If an object is not escaped in the method, it can also be allocated on the stack for allocation.
⑤④. What are stored in the method area?
- Type information, constants, static variables, just-in-time compiler compiled code cache
⑤⑤. The size of the method area determines how many classes the system can save. If the system defines too many classes, the method area will overflow, and the virtual machine will also throw a memory overflow error?
①. Load a large number of third-party jar packages
②. Tomcat deployment projects are too many (30-50)
③. A large number of dynamically generated reflection classes
⑤⑥. Talk about your changes to the string constant pool and static variables in the method area?
①. Jdk 1.6 and before: there is a permanent generation, static variables, string constant pool 1.6 in the method area
②. Jdk 1.7: There is permanent generation, but it has been gradually "de-permanent generation", string constant pool, static variables are removed, and stored in the heap
③. jdk 1.8 and later: There is no permanent generation, and the constant pool 1.8 is in the meta space. But static variables and string constant pools are still in the heap
⑤⑦. Why use metaspace to replace permanent generation
①. The permanent generation parameter setting is too small. In some scenarios, if there are too many dynamically loaded classes, it is easy to produce OOM in the Perm area. For example, in an actual web project, because there are many function points, it is necessary to Constantly loading many classes dynamically, and fatal errors often occur
②. Permanent generation parameters are set too large, resulting in waste of space
③. By default, the size of the meta space is limited by the local memory)
④. It is very difficult to tune the permanent generation
[Garbage collection in the method area mainly recycles two parts: the discarded constants in the constant pool and the no longer used types, and the no longer used classes or class loader recycling is more complicated ,full gc time is long]
⑤⑧. Why should StringTable be adjusted?
①. Put StringTable in the heap space in jdk7. Because the collection efficiency of the permanent generation is very low, it can only be triggered when it is full gc. And full gc is triggered by insufficient space in the old age and insufficient permanent generation.
②. This leads to StringTable recycling efficiency is not high, and a large number of strings will be created in our development, recycling efficiency is low, resulting in insufficient permanent generation memory, put it in the heap, can reclaim the memory in time