Check the memory usage of Java programs deployed in docker

question

  • Our spring boot programs are all mirrored and deployed in docker
  • A microservice has a relatively simple function and a small number of users, but it is found that the memory occupies 2G during the startup process
  • Now you need to check the memory usage

deal with

  • Check the memory usage of the Java program in docker, which is actually the same as the Java program directly deployed and started in Linux
  • Here we use some commands that come with Java to check, first enter the dockerdocker exec -it xxx /bin/bash
  • Use jstackto check the thread usage, it turns out that bash: jstack: command not foundyou can't find the command under the java folder
  • Here you need to pay attention, your Java program image, jdkyou still jre, because these commands are available in jdk, and you can't use jre to build them. For debugging here, you can change the image to jdk first, and then change it back after debugging
  • Continue to debug, use jmapto view the stack, and find the following error. Here you should pay attention to whether the user you enter docker is the starting user, the user is inconsistent, and cannot be executed
 jmap -dump:format=b,file=core.dump 1
Picked up _JAVA_OPTIONS: -Xmx1G -Xms1G
Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file /proc/1/root/tmp/.java_pid1: target process 1 doesn't respond within 10500ms or HotSpot VM not loaded
	at jdk.attach/sun.tools.attach.VirtualMachineImpl.<init>(VirtualMachineImpl.java:100)
	at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:58)
	at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
	at jdk.jcmd/sun.tools.jmap.JMap.executeCommandForPid(JMap.java:128)
	at jdk.jcmd/sun.tools.jmap.JMap.dump(JMap.java:208)
	at jdk.jcmd/sun.tools.jmap.JMap.main(JMap.java:114)
  • Want to generate a dump file, but found that there is no write file permission
I have no name!@d834ffe5656e:/$ jmap -dump:format=b,file=core.dump 1
Picked up _JAVA_OPTIONS: -Xmx1G -Xms1G
Dumping heap to /core.dump ...
Unable to create /core.dump: Permission denied
  • Use the root account to enter docker ( docker exec -u 0 -it xxx /bin/bash), set permissions for the java installation directory (use java -version to view the version), the java bin directory, and the jmap command file in the java bin directory, and enter the following three commands in the corresponding directory
chmod 777 jdk
chmod 777 bin
chmod 777 jmap
  • No problem, you can use the command and output file normally
  • After the file is generated, exit docker and use the command to get the dump file to the hostdocker cp 容器id:/位置 位置

Guess you like

Origin blog.csdn.net/u010882234/article/details/128890127