高内存占用,堆溢出,OOM
代码:
@RequestMapping(value = "/oom", method = {RequestMethod.GET})
public ResultBase getMessage2() throws InterruptedException {
List<String> strList = Lists.newArrayList();
for (int i = 0; i < 10240; i++) {
strList.add("hongdaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + i);
}
Thread.sleep(600000);
return ResultBase.getSuccessResult(ResultDTO.of().setResult(null));
}
内存泄露:申请使用完的内存没有释放,导致虚拟机不能再次使用该内存
内存溢出:申请的内存超出了JVM
能提供的内存大小,此时称之为溢出。
服务启动:
nohup java -Dserver.port=8070 -Xms200m -Xmx200m -verbose:gc -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom_dump.hprof -Dfile.encoding=UTF-8 -jar chatgpt-web-0.0.1-SNAPSHOT.jar >info.log 2>&1 &
jmap
查看:
❯ jmap -heap 885
Attaching to process ID 885, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.251-b08
....
多次调用接口,生成大对象:
curl http://172.29.168.205:8070/oom
生成hrpof
:
jmap -dump:live,format=b,file=heapdump.hprof 885
查看最大对象:
查看大对象传出引用:
查看大对象传入引用:
CPU满载
代码:
@RequestMapping(value = "/cpu", method = {RequestMethod.GET})
public ResultBase cpu() throws InterruptedException {
long i = 0;
while (true) {
i++;
if (i == 999999123456L) {
break;
}
}
return ResultBase.getSuccessResult(ResultDTO.of().setResult(null));
}
使用top命令查看cpu过高的pid
查看cpu过高的线程id tid
top -Hp 30318
or
top 然后 Shift+H
cpu过高的线程tid:30394
打印java堆栈跟踪信息到stack.txt文件
jstack 30318 >stack.txt
转换线程id为16进制格式
❯ printf '%x\n' 30394
76ba
筛选查看cpu过高的堆栈信息
❯ rg 76ba stack.txt -A20 -B 20
149- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
150- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
151- at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
152- at java.lang.Thread.run(Thread.java:748)
153-
154-"http-nio-8070-exec-2" #27 daemon prio=5 os_prio=0 tid=0x00007fad350f9000 nid=0x76bb waiting on condition [0x00007facad5d3000]
155- java.lang.Thread.State: WAITING (parking)
156- at sun.misc.Unsafe.park(Native Method)
157- - parking to wait for <0x00000000faa27200> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
158- at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
159- at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
160- at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
161- at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:108)
162- at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
163- at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
164- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
165- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
166- at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
167- at java.lang.Thread.run(Thread.java:748)
168-
169:"http-nio-8070-exec-1" #26 daemon prio=5 os_prio=0 tid=0x00007fad350f6000 nid=0x76ba runnable [0x00007facad6d1000]
170- java.lang.Thread.State: RUNNABLE
171- at com.shanjige.chatgpt.chatgptweb.web.IndexController.cpu(IndexController.java:64)
172- at com.shanjige.chatgpt.chatgptweb.web.IndexController$$FastClassBySpringCGLIB$$db8434c4.invoke(<generated>)
173- at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
174- at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
175- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
176- at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
177- at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
178- at com.shanjige.chatgpt.chatgptweb.aop.ControllerAspect.aroundAdvice(ControllerAspect.java:71)
179- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
180- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
181- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
182- at java.lang.reflect.Method.invoke(Method.java:498)
标签:java,Thread,util,concurrent,内存,org,JAVA,CPU,ThreadPoolExecutor
From: https://www.cnblogs.com/hongdada/p/17528666.html