jvm learning summary
1. Program counter
2. Virtual machine stack
1. Definition
Control stack size
Xss256kb // Command line parameters
2.2 thread diagnosis
top Command view cpu Operation ps H -eo pid,tid,%cpu |grep process ID
2.3 thread deadlock
void function(){ while(true){ // The method runs in a loop } }
3. Local method stack
The method modified by native in Java shows that this method is in the local method area.
public class Object{ public native Object clone(){ } }
4. Pile
4.1 definition
Controls the size of heap memory
Xmx8m
4.2 heap memory overflow
4.3 heap memory diagnosis
package com.bubaiwantong.heap; import java.util.ArrayList; public class Demo02 { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); try { Thread.sleep(30000); for (int i = 0; i < 200; i++) { Student student = new Student(); students.add(student); } Thread.sleep(300000); }catch (Exception e){ e.printStackTrace(); } } } class Student{ private byte[] info = new byte[10 * 1024 * 1024]; }
5. Method area
5.1 definition
5.2. JDK version comparison
- Before 1.8, the method area was inside the JVM
- 1.8 anti emetic method area becomes meta space
Permanent generation
- -XX:MaxPermSize=8m
package com.bubaiwantong.method; import jdk.internal.org.objectweb.asm.ClassWriter; import jdk.internal.org.objectweb.asm.Opcodes; /** * Configure VM parameters * -XX:MaxPermSize=8m */ public class Demo02 extends ClassLoader{ public static void main(String[] args) { int count=0; try { Demo02 test = new Demo02(); for (int i = 0; i < 10000; i++,count++) { ClassWriter cw = new ClassWriter(0); cw.visit(Opcodes.V1_8,Opcodes.ACC_PUBLIC,"Class" + i,null,"java/lang/Object",null); byte[] code = cw.toByteArray(); test.defineClass("Class" + i,code,0,code.length); } }finally { System.out.println(count); } } }
Meta space
- Configure VM parameters
- -XX:MaxMetaspaceSize=8m
package com.bubaiwantong.method; import jdk.internal.org.objectweb.asm.ClassWriter; import jdk.internal.org.objectweb.asm.Opcodes; /** * Configure VM parameters * -XX:MaxMetaspaceSize=8m */ public class Demo01 extends ClassLoader{ public static void main(String[] args) { int count=0; try { Demo01 test = new Demo01(); for (int i = 0; i < 10000; i++,count++) { ClassWriter cw = new ClassWriter(0); cw.visit(Opcodes.V1_8,Opcodes.ACC_PUBLIC,"Class" + i,null,"java/lang/Object",null); byte[] code = cw.toByteArray(); test.defineClass("Class" + i,code,0,code.length); } }finally { System.out.println(count); } } }
5.4. Runtime constant pool
- The constant pool is a table. The virtual machine instruction finds the class name, method name, parameter type, literal and other information to be executed according to the constant table
- Runtime constant pool, constant pool is * In the class file, when the class is loaded, its constant pool information will be put into the runtime constant pool, and the symbolic address in it will be changed into the real address.
5.5. StringTable feature
Explanation of string pool If you don't understand, you can listen to this course
package com.bubaiwantong.compile; public class Demo03 { public static void main(String[] args) { // ["ab","a","b"] String x = "ab"; String s = new String("a") + new String("b"); // Heap new String("a") new String("b") new String("ab") String s2= s.intern(); // If there is no string, the string object will be put into the pool. If there is no attempt, the string object will be put into the pool. System.out.println(s2 == x); // The true string pool already has "ab", so it is directly taken out, which is equal to x System.out.println(s == x); // false This is an object of new String("ab"), so it is not equal to "ab" // In the final analysis, let's look at what = = compares, = = determines whether the addresses of two objects are equal, not whether the values of two objects are equal. } }
1) Change position
String x2=new String("c")+new String("d"); x2.intern(); // Put "cd" into the string pool, and then x1 will take "cd" out of the string pool, so these are the same two objects. String x1= "cd"; System.out.println(x1 == x2); // true
2) JDK1.6
// jdk 1.6 String x2=new String("c")+new String("d"); x2.intern(); // A copy of "cd" will be copied to the string pool, that is, a copy will be copied to the string pool while you are still in the heap, so the two x1 and x2 are not one object String x1= "cd"; System.out.println(x1 == x2); // false
To sum up: intern() in jdk1 6. If there is no string in the string pool, a copy will be copied, that is, an object will be created, so the address value will be different.
And in jdk1 When 8 is executed, the function intern() will put x2 in itself, so when x1 gets it, x1 and X2 will be equal.
6. Direct memory
6.1 definition
- Direct memory can be called shared memory, which can be accessed by Java virtual machine and operating system
6.2 distribution and recovery principle
- The Unsafe object is used to complete the allocation and recycling of direct memory, and the recycling needs to actively call the freeMemory method
- Inside the ByteBuffer implementation class, Cleaner (Virtual Reference) is used to monitor the ByteBuffer object. Once the ByteBuffer object is garbage collected, the ReferenceHandler thread will call freeMemory through the clean method of Cleaner to release the direct memory.
import sun.misc.Unsafe; import java.io.IOException; import java.lang.reflect.Field; /** * Direct memory can be called shared memory, which can be accessed by Java virtual machine and operating system */ public class Demo03 { static int _1GB = 1024*1024*1024; public static void main(String[] args) throws IOException { Unsafe unsafe= getUnsafe(); long base = unsafe.allocateMemory(_1GB); // Gets the address of the allocated memory unsafe.setMemory(base,_1GB,(byte ) 0); // Allocate memory System.in.read(); unsafe.freeMemory(base); // Free memory System.in.read(); } public static Unsafe getUnsafe(){ try { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); Unsafe unsafe = (Unsafe ) f.get(null); return unsafe; } catch (Exception e) { e.printStackTrace(); } return null; } }
- Disable garbage collection for display
-Xx:+DisableExplicitGC //Displayed