【死磕JVM】看完这篇我也会排查JVM内存过高了 就是玩儿!_refrence handler blocked

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

    List<CardInfo> taskList = new ArrayList<>();
    //每次查询100张卡出来
    for (int i = 0; i < 100; i++) {
        CardInfo ci = new CardInfo();
        taskList.add(ci);
    }

    return taskList;
}

}


程序的设计其实比较简单,就是我们用信用卡的案例来进行说明,比如CardInfo就是信用卡类,我们把这个人对应的信用卡的记录都调用出来,之后做一些自己响应的业务处理方法来对它进行处理和计算,来看看我们这个模型是否符合modelFit,具体怎么做呢,在应用程序中有一个类是CardInfo,有一个方法叫做getAllCardInfo,每次都是拿100个出来,拿100个之后用线程池做计算,线程池用的是`ScheduledThreadPoolExecutor (定时任务)`,new出来线程池之后,50个线程池,然后做对应的业务逻辑处理,会调用 modelFit(),使用100毫秒模拟业务的停顿。


首先我们需要使用javac 命令将Java文件进行编译  
 `javac FullGCTest.java`进行编译,然后打印GC日志,进行风险监控


打印GC日志:`java -Xms200M -Xmx200M -XX:+PrintGC FullGCTest`


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417103603145.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE0OTk2NDIx,size_16,color_FFFFFF,t_70)


### 怎么知道JVM内存过高?


在公司里面,如果遇到了JVM内存过高的情况,那么一般是运维团队首先受到报警信息,然后通知对应的开发人员去查看,那么开发人员应该如何查看,或者怎么样去排查呢?


##### 1、top 查看进程


受到报警信息后,拿top命令去查询


`[root@root ~]# top`  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417103138943.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE0OTk2NDIx,size_16,color_FFFFFF,t_70)


查看内存不断增长,CPU占用率居高不下的。top后你会看到它的PID(31061)。它占比比较高。


##### 2、top -Hp 查看线程


找到CPU占用比较高的进程PID,这里我们以 java 的进程为例  
 使用命令 `top -Hp 31061`,这个时候它会把这个进程里面所有的线程全部线程都罗列出来吗,这些都是Java这个进程里面内部的一些线程,如下图所示:


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417103221476.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE0OTk2NDIx,size_16,color_FFFFFF,t_70)


我们会看到每个线程的占比都差不多,偶尔会有某一个线程比较高,在某些线程占得比较高的时候,这个小例子最终会是垃圾回收的线程占得比较高,因为垃圾回收不过来了,所以需要不停的来回回收,每次都回收一点点,实际这种例子里面非常有可能是你业务逻辑线程,那一块的业务逻辑线程占比非常高,这是时候就需要用到另外的命令——`jstack`


##### 3、jstack


当我们使用 top -Hp 知道了是哪个线程后,我们下一步就可以使用 `jstack`命令,比如我们要查看31083这个线程号,31061是我们的进程PID,我们要定位某一个线程cpu的占比会比其他cpu高很多,那么我们就要定位这个线程里面到底是什么样的问题的时候,就需要把这个线程号(31083)记下来。


因为 **jstack** 用到的线程号是16进制的,所以我们需要把31083的10进制转换成16进制才可以


**特点:**


* 每个线程有自己的线程号码,里面有线程的状态,可以观察线程是否阻塞,如果长时间的wait和block说明这个线程是有问题的


##### 4、转换16进制


因为Java线程文件中的线程ID是16进制,所以需要将线程ID从十进制转换成十六进制  
 命令:`echo "obase=16;31083" | bc`  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021041611313755.png)


##### 5、jstack用法解析



[root@root ~]# jstack
Usage:
jstack [-l]
(to connect to running process)
jstack -F [-m] [-l]
(to connect to a hung process)
jstack [-m] [-l]
(to connect to a core file)
jstack [-m] [-l] [server_id@]
(to connect to a remote debug server)

Options:
-F to force a thread dump. Use when jstack 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


##### 6、jstack查看输出


我们也可以用 `jps或者java ps -ef| java`来查看Java进程,这里我们用jps来查看


`[root@root ~]# jps`


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417102330485.png)  
 `[root@root ~]# jstack 31061`



“pool-1-thread-3” #10 prio=5 os_prio=0 tid=0x00007f3568105800 nid=0x7961 waiting on condition [0x00007f35455cf000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f8a81148> (a java.util.concurrent.locks.AbstractQueuedSynchronizer C o n d i t i o n O b j e c t ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . L o c k S u p p o r t . p a r k ( L o c k S u p p o r t . j a v a : 175 ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . A b s t r a c t Q u e u e d S y n c h r o n i z e r ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer ConditionObject)atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:175)atjava.util.concurrent.locks.AbstractQueuedSynchronizerConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.ScheduledThreadPoolExecutor D e l a y e d W o r k Q u e u e . t a k e ( S c h e d u l e d T h r e a d P o o l E x e c u t o r . j a v a : 1088 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088) at java.util.concurrent.ScheduledThreadPoolExecutor DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)atjava.util.concurrent.ScheduledThreadPoolExecutorDelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

“pool-1-thread-2” #9 prio=5 os_prio=0 tid=0x00007f3568103800 nid=0x7960 waiting on condition [0x00007f35456d0000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f8a81148> (a java.util.concurrent.locks.AbstractQueuedSynchronizer C o n d i t i o n O b j e c t ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . L o c k S u p p o r t . p a r k ( L o c k S u p p o r t . j a v a : 175 ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . A b s t r a c t Q u e u e d S y n c h r o n i z e r ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer ConditionObject)atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:175)atjava.util.concurrent.locks.AbstractQueuedSynchronizerConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.ScheduledThreadPoolExecutor D e l a y e d W o r k Q u e u e . t a k e ( S c h e d u l e d T h r e a d P o o l E x e c u t o r . j a v a : 1088 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088) at java.util.concurrent.ScheduledThreadPoolExecutor DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)atjava.util.concurrent.ScheduledThreadPoolExecutorDelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

“pool-1-thread-1” #8 prio=5 os_prio=0 tid=0x00007f3568102000 nid=0x795f waiting on condition [0x00007f35457d1000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f8a81148> (a java.util.concurrent.locks.AbstractQueuedSynchronizer C o n d i t i o n O b j e c t ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . L o c k S u p p o r t . p a r k ( L o c k S u p p o r t . j a v a : 175 ) a t j a v a . u t i l . c o n c u r r e n t . l o c k s . A b s t r a c t Q u e u e d S y n c h r o n i z e r ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer ConditionObject)atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:175)atjava.util.concurrent.locks.AbstractQueuedSynchronizerConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.ScheduledThreadPoolExecutor D e l a y e d W o r k Q u e u e . t a k e ( S c h e d u l e d T h r e a d P o o l E x e c u t o r . j a v a : 1088 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088) at java.util.concurrent.ScheduledThreadPoolExecutor DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)atjava.util.concurrent.ScheduledThreadPoolExecutorDelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

“Service Thread” #7 daemon prio=9 os_prio=0 tid=0x00007f35680b4000 nid=0x795d runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

“C1 CompilerThread1” #6 daemon prio=9 os_prio=0 tid=0x00007f35680b1000 nid=0x795c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

“C2 CompilerThread0” #5 daemon prio=9 os_prio=0 tid=0x00007f35680af000 nid=0x795b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

“Signal Dispatcher” #4 daemon prio=9 os_prio=0 tid=0x00007f35680ad800 nid=0x795a runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

“Finalizer” #3 daemon prio=8 os_prio=0 tid=0x00007f356807c800 nid=0x7959 in Object.wait() [0x00007f3558301000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f8a86b38> (a java.lang.ref.ReferenceQueue L o c k ) a t j a v a . l a n g . r e f . R e f e r e n c e Q u e u e . r e m o v e ( R e f e r e n c e Q u e u e . j a v a : 144 ) − l o c k e d < 0 x 00000000 f 8 a 86 b 38 > ( a j a v a . l a n g . r e f . R e f e r e n c e Q u e u e Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000000f8a86b38> (a java.lang.ref.ReferenceQueue Lock)atjava.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)locked<0x00000000f8a86b38>(ajava.lang.ref.ReferenceQueueLock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

“Reference Handler” #2 daemon prio=10 os_prio=0 tid=0x00007f3568077800 nid=0x7958 in Object.wait() [0x00007f3558402000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f8a86cf0> (a java.lang.ref.Reference L o c k ) a t j a v a . l a n g . O b j e c t . w a i t ( O b j e c t . j a v a : 502 ) a t j a v a . l a n g . r e f . R e f e r e n c e . t r y H a n d l e P e n d i n g ( R e f e r e n c e . j a v a : 191 ) − l o c k e d < 0 x 00000000 f 8 a 86 c f 0 > ( a j a v a . l a n g . r e f . R e f e r e n c e Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x00000000f8a86cf0> (a java.lang.ref.Reference Lock)atjava.lang.Object.wait(Object.java:502)atjava.lang.ref.Reference.tryHandlePending(Reference.java:191)locked<0x00000000f8a86cf0>(ajava.lang.ref.ReferenceLock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

“main” #1 prio=5 os_prio=0 tid=0x00007f3568009800 nid=0x7956 waiting on condition [0x00007f356ed59000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at FullGCTest.main(FullGCTest.java:35)

“VM Thread” os_prio=0 tid=0x00007f356806e000 nid=0x7957 runnable

“VM Periodic Task Thread” os_prio=0 tid=0x00007f35680b7000 nid=0x795e waiting on condition

JNI global references: 205


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417104709653.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE0OTk2NDIx,size_16,color_FFFFFF,t_70)  
 **通过thread dump分析线程状态**


大多数情况下会基于thead dump 分析当前各个线程的运行情况,如是否存在死锁,是否存在一个线程长时间持有锁不释放等等


在dump中,线程一般存在如下几种状态:  
 1、RUNNABLE,线程处于执行中  
 2、BLOCKED,线程被阻塞  
 3、WAITING,线程正在等待


`locked <0x000000076bf62208>`说明线程 对地址为`0x000000076bf62208`对象进行了加锁;  
 `waiting to lock <0x000000076bf62208>`说明线程 在等待地址为`0x000000076bf62208`对象上的锁;  
 `waiting for monitor entry [0x000000001e21f000]`说明线程 是通过synchronized关键字进入了监视器的临界区,并处于"Entry Set"队列,等待monitor;  
 `waiting on <0x0000000088ca3310> (a java.lang.Object)`等待锁的释放


假如有一个进程中有100个线程,很多线程都在waiting on 某一把锁,然后线程不该阻塞的被阻塞了,该结束的没结束掉,一定要找到哪个线程持有这把锁 ,我们可以搜索 jstack dump 的信息,找到`<0X...>`的信息,看哪个线程只有了这把锁,一般这个线程状态是RUNNABLE,表示这个线程正在运行但是一直持有这把锁不释放,那么就会导致整个线程的死锁


##### 7、jstack分析死锁



public class TestDeadLock {

private static Object obj1 = new Object();
private static Object obj2 = new Object();

public static void main(String[] args) {
    new Thread(new Thread1()).start();
    new Thread(new Thread2()).start();
}

private static class Thread1 implements Runnable {
    @Override
    public void run() {
        synchronized (obj1) {
            System.out.println("Thread1 拿到了 obj1 的锁!");
            try {// 停顿2秒的意义在于,让Thread2线程拿到obj2的锁
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (obj2) {
                System.out.println("Thread1 拿到了 obj2 的锁!");
            }
        }
    }
}


private static class Thread2 implements Runnable {
    @Override
    public void run() {
        synchronized (obj2) {
            System.out.println("Thread2 拿到了 obj2 的锁!");
            try {
                // 停顿2秒的意义在于,让Thread1线程拿到obj1的锁
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (obj1) {
                System.out.println("Thread2 拿到了obj1的锁!");
            }
        }
    }
}

}


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210417132904207.png)  
 通过命令查看分析日志



[root@root fuccGC]# jps
485 Bootstrap
9877 Jps
10629 QuorumPeerMain

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

g-HjVMEczU-1715790976929)]
[外链图片转存中…(img-pJOgGXpq-1715790976930)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值