jmap:java内存映像工具
jmap用于生成堆转储快照,比较常用的option包括-heap,-histo,-dump
[root@localhost script]# jmap -h Usage: jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to connect to a core file) jmap [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server) where <option> is one of: <none> to print same info as Solaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -clstats to print class loader statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system
java -heap [vmid],可以打印制定java虚拟机进程id的堆基本信息及使用情况,包括新生代大小、老年代大小、使用空间大小及比例。
jmap -histo [vmid],可以打印矩阵图,可以看到java对象实例数量、大小、及具体是哪个对象。
jmap -dump:live,format=b,file=heap.bin <vmid>,可以生成一个heap.bin文件,从服务器上拿下来通过MAT工具进行分析,找到占用内存较高的对象,可以定位内存溢出的错误。
总体来说jmap命令操作简单,但是用来优化系统和排查问题最常用的命令工具。
jstack:线程堆栈信息查看工具
jstack是jdk自带的线程堆栈分析工具,使用该命令可以查看或导出 java 应用程序中线程堆栈信息。
[root@localhost script]# jstack -h Usage: jstack [-l] <pid> (to connect to running process) jstack -F [-m] [-l] <pid> (to connect to a hung process) jstack [-m] [-l] <executable> <core> (to connect to a core file) jstack [-m] [-l] [server_id@]<remote server IP or hostname> (to connect to a remote debug server) Options: -F to force a thread dump. Use when jstack <pid> does not respond (process is hung) -m to print both java and native frames (mixed mode) -l long listing. Prints additional information about locks -h or -help to print this help message
jstack可以用来排查CPU使用高、内存使用高的情况,最终定位到问题点。
通过top命令查看CPU(内存)占用高的进程ID,然后通过命令top -H -p <vmid>,定位到具体的线程号pid。
拿到pid后,转为16进制,通过命令printf "%x\n" pid,拿到hexpid
然后通过jstack <vmid> | grep <hexpid> -A 30,最终定位到问题点。
jstack的使用例子:
我首先创建了一个很简单的springboot的项目,然后提供了一个接口,接口的方法体中有个双层循环(方法代码见下面的mockdump),把该项目部署到linux服务器上,启动后的进程id是73165,执行shell命令top -H -p 73165,然后通过浏览器访问该方法。之后观察到线程号73183的cpu使用率高达95%(见下图),然后通过jstack查看线程堆栈信息(见最后的shell命令执行的结果),发现异常信息指向了64行双层循环处,即定位到了代码异常点。
@GetMapping("/mockdump") public String mockdump(String loopcount) throws InterruptedException, ExecutionException { log.info("===mockdump===start==="); for (int i = 0; i < Integer.parseInt(loopcount); i++) { ExecutorService executor = new ThreadPoolExecutor(2, 10, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); Future<String> f = executor.submit(new Callable<String>() { @Override public String call() throws Exception { log.info("===wolaile==="); return "wolaile"; } }); long result = 0l; for (int m = 0; m < 999999; m++) {//这个是当前类的第64行 for (int j = 0; j < 999999999; j++) { result = result + j; } } calllist.add(f.get()); } log.info("===mockdump===end==="); return "success"; }
[root@localhost ~]# printf "%x\n" 73183 11ddf [root@localhost ~]# jstack 73165 | grep 11ddf -A 30 "http-nio-8080-exec-1" #13 daemon prio=5 os_prio=0 tid=0x00007fb33cdd6000 nid=0x11ddf runnable [0x00007fb30e6e7000] java.lang.Thread.State: RUNNABLE at com.demo.incubator.swaggerdemo.controller.SwaggerDemoController.mockdump(SwaggerDemoController.java:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)