Deep jvm Kernel-Principle, Diagnosis and Optimization-3. Common jvm configuration parameters

Keywords: Programming Java jvm Windows Apache

1. Common configuration parameters for jvm

  1. In fact, from and to are just logical concepts. Physically, the new generation is allocating the memory of the object + the memory space of the object to be copied

     -XX:+PrintGCDetails
     -XX:+PrintGCDetails This is printed every gc, but the detailed heap information is not printed until after the program is finished
      -Xmx does not contain, persistent generation space
     Heap space is continuous
    
  2. Streamline jre

     First learn that Java runs mainly from the bin and Lib directories, which store Java commands and required DLLs
     The lib directory is the class and configuration file used by the java virtual machine.
     How to streamline the bin directory:
     1. The main tool of the bin directory is java.exe, which executes the class file.
     Other executables are generally not needed (excludable) if you are just running Java programs.
    
     2. The dll file in the bin directory is called during the execution of the class file by java.exe.
     Execute the class file, which library file java.exe needs to load which dll, which can be excluded if not needed.
     To see which dll java uses, you can use windows Task Manager, view the process number, and use other tools (such as 360)
     View referenced DLLs
    
     lib streamlining:
     Here I'm going to give you a simplified rt.jar program, written by myself. (This is mainly about the simplified rt.jar program.)
     The main idea is:
     1. Print the class file needed to run the program to a text file through -XX:TraceClassLoading
     2. Use your own programs to set the required class es and rt paths, and simplify the path where RTs are stored
     3. Then pack the directories and files inside rt1 into rt.zip, rename it rt.jar, and replace the original rt.jar
     4. You can simplify it and copy Java.exe and the corresponding DLL to the appropriate directory.
     5. Write a batch command to execute the jar package in your own Java.
    
  3. A tortuous journey to find the way

    1. rt.jar simplification not needed for this main function

      
      	package com.tencent.tubemq.example;
      
      
      	import java.util.Vector;
      
      	/**
      	 * description:
      	 *
      	 * @author: dawn.he QQ:       905845006
      	 * @email: dawn.he@cloudwise.com
      	 * @email: 905845006@qq.com
      	 * @date: 2019/9/22    8:44 AM
      	 */
      	public class PermOOM {
      
      		public static void main(String[] args) {
      
      			Vector v=new Vector();
      			for(int i=0;i<10;i++)
      				v.add(new byte[1*1024*1024]);
      		        System.out.println("-------");
      		}
      	}
      

    1. (upper main function) required class generation

     implement
     java -XX:+TraceClassLoading  com.tencent.tubemq.example.PermOOM>a.log
    
  4. RT store path (lib rt.jar in jre)

  5. Reduced path ls

  6. Compressed file

     jar cvf rt.jar *
    
     Compressed rt.jar replaces rt.jar under lib in jre remembering to back up
     This rt.jar can only run in the same environment as the main function dependency above. It is not generic. The test successfully remembers to replace it. The main purpose of this is to reduce the package size of jre in an online environment or when deploying to customers.
    
  7. Execute under classes

     java  com.tencent.tubemq.example.PermOOM
    

    1. Tool class

      	package com.tencent.tubemq.example;
      
      	/**
      	 * description:jar cvf rt.jar *
      	 *
      	 * @author: dawn.he QQ:       905845006
      	 * @email: dawn.he@cloudwise.com
      	 * @email: 905845006@qq.com
      	 * @date: 2019/9/22    1:39 PM
      	 */
      
      	import java.io.BufferedReader;
      	import java.io.File;
      	import java.io.FileInputStream;
      	import java.io.FileOutputStream;
      	import java.io.IOException;
      	import java.io.InputStream;
      	import java.io.InputStreamReader;
      	import java.util.jar.JarEntry;
      	import java.util.jar.JarFile;
      
      	import org.apache.commons.io.IOUtils;
      
      	//Write by Yourself
      	public class CutJre {
      		private static String needClazz = "/Users/heliming/IdeaProjects/TubeMQ/tubemq-example/target/classes/a.log";//Required class
      		private String rtPath = "/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib";//rt store path
      		private String dstRtPath = "./cutJar/";//Reduced path ls
      		private JarFile rtJar;
      
      		public static void main(String[] args) throws Exception {
      
      
      			CutJre cutJre = new CutJre();
      			cutJre.rtJar = new JarFile(cutJre.rtPath + "/rt.jar");
      			BufferedReader br = new BufferedReader(new InputStreamReader(
      					new FileInputStream(needClazz)));
      			String string = br.readLine();
      			while (string != null) {
      				string = br.readLine();
      
      				cutJre.copyClass(string);
      			}
      	//		 cutJre.execute();
      		}
      
      		private void execute() throws Exception {
      			BufferedReader br = new BufferedReader(new InputStreamReader(
      					new FileInputStream(needClazz)));
      			String string = br.readLine();
      			while (string != null) {
      				string = br.readLine();
      			}
      		}
      
      		private boolean copyClass(String traceStr) throws IOException {
      			//Not the Jar package inside rt, it's your own
      			if (traceStr == null || !traceStr.contains("/jre/lib/rt.jar")) {
      				return true;
      			}
      			if (traceStr.startsWith("[Loaded")) {
      				String className = traceStr.split(" ")[1];
      
      				copyFile(className);
      			}
      			return false;
      		}
      
      		private void copyFile(String className) throws IOException {
      			String classFile = className.replace(".", "/") + ".class";
      			String classDir = classFile.substring(0, classFile.lastIndexOf("/"));
      
      			File dir = new File(dstRtPath + classDir);
      			System.out.println(dir);
      			if (!dir.exists()) {
      				dir.mkdirs();
      			}
      			JarEntry jarEntry = rtJar.getJarEntry(classFile);
      			InputStream ins = rtJar.getInputStream(jarEntry);
      			File file = new File(dstRtPath + classFile);
      			System.out.println(file);
      			if (!file.exists()) {
      				file.createNewFile();
      			}
      			FileOutputStream fos = new FileOutputStream(file);
      			IOUtils.copy(ins, fos);
      			ins.close();
      			fos.close();
      
      		}
      	}
      

      jar package with io dependency in tool class pom file

      <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
       <version>2.5</version>
      </dependency>
      
  8. Ignore this one for my record

     Start JVM parameters considered most likely:
     -Xms25m -Xmx40m -Xmn7m -XX:+PrintGCDetails -XX:PermSize=16m
    
     First def new generation total 646464K, used 115K [0x34e80000, 0x35580000, 0x35580000)
      eden space 5760K,   2% used [0x34e80000, 0x34e9cd38, 0x35420000)
       from space 704K,   0% used [0x354d0000, 0x354d0000, 0x35580000)
       to   space 704K,   0% used [0x35420000, 0x35420000, 0x354d0000)
     From this line you can see that the younger generation is 7m in size.
    
     By tenured generation total 18124K, used 8277K [0x35580000, 0x36733000, 0x37680000]
    
     (0x37680000-0x35580000)/1024/1024 The result is 33m
    
     From the above, you can get a maximum heap of 40m.However, the minimum heap size calculated from the eden size and tended generation total 18124K should be 25m
    
     Persistent heap-XX:PermSize=16m can be calculated by compacting perm Gen total 16384K
    
  9. Print stack error information to log

     	Also in classes Down Execution
    
     java -Xmx20m -Xms5m  -XX:+HeapDumpOnOutOfMemoryError com.tencent.tubemq.example.PermOOM>c.log
    

    1. Print gc logs and export dump files

     -Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:+TraceClassLoading -XX:HeapDumpPath=/Users/heliming -Xloggc:./gc.log
    

    Generate gc.log on this path in the / Users/heliming directory

     Find / -name'jre'searches the directory where JRE is located
    
  10. jvisualvm

  11. Import.hprof suffix file

1. Start the analysis

Combining code

	TotMemory (): Returns the total amount of memory in the Java virtual machine.
	maxMemory(): Returns the maximum amount of memory the Java virtual machine is trying to use.
	freeMemory(): Returns the amount of free memory in the Java virtual machine.

True situation

  1. Heap memory allocation

  2. This is a nice look at jdk6. It's not the same name as jdk8, but it's just a different name

    1. If it's jdk8, change 3 to 4 here
  3. summary

Posted by dstantdog3 on Sun, 22 Sep 2019 19:51:15 -0700