zoukankan      html  css  js  c++  java
  • 6、使用jconsole+VisualVM分析JVM

    一、不断增加对象触发GC的代码

    VM 参数:-Xms100m -Xmx100m -XX:+UseSerialGC

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            while (true){
                fillheap(1000);
    
            }
    
    
    
        }
    
        private static void fillheap(int count) throws InterruptedException {
            List<OOMObjcet> list = new ArrayList<OOMObjcet>();
            for (int i = 0; i<count ; i++){
                Thread.sleep(50);
                list.add(new OOMObjcet());
            }
            System.gc();
    
        }
        static class OOMObjcet{
            public byte[] placeholder = new byte[64*1024];
        }
    }

    效果图:

    二、线程检测

    代码:

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class ThreadDemo {
    
        public static void createBusyThread(){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true);
                }
            },"testBusyThread");
            thread.start();
        }
    
        public static void createLockThread(final  Object lock){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock){
                        try {
                            lock.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            },"testLockThread");
            thread.start();
        }
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
            br.readLine();
            createBusyThread();
            br.readLine();
    
            Object o = new Object();
            createLockThread(o);
        }
    }

    jconsole的图:

    三、死锁代码

    public class ThreadDieLock {
        static class SynAddRunable implements Runnable{
            int a,b;
    
            public SynAddRunable(int a, int b) {
                this.a = a;
                this.b = b;
            }
    
            @Override
            public void run() {
                synchronized (Integer.valueOf(a)){
                    synchronized (Integer.valueOf(b)){
                        System.out.println(a+b);
                    }
                }
            }
        }
    
        public static void main(String[] args) {
    
            for (int i = 0;i <100;i++){
                new Thread(new SynAddRunable(1,2)).start();
                new Thread(new SynAddRunable(2,1)).start();
            }
        }
    }

    二、VisualVM

    VisualVM安装插件报错

    https://blog.csdn.net/xionglangs/article/details/77603343

  • 相关阅读:
    php_package v2.7发布了 宋正河作品
    svn图文教程-宋正河整理
    ci框架学习整理
    php+mysql 数据库分表分段备份程序--宋正河
    保留mysql数据库中的最新1000条记录
    php 文件上传缩略图路径分析类
    php js css加载合并函数 宋正河整理
    二级域名 cookie session 共享
    图像处理相关概念
    由Python到深度学习入门之Keras、TensorFlow 、PyTorch联系与区别
  • 原文地址:https://www.cnblogs.com/yeyongjian/p/9247565.html
Copyright © 2011-2022 走看看