栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【初级03】java JVM核心技术(3): jvm调优

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【初级03】java JVM核心技术(3): jvm调优

本文使用的JDK版本:

$ java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

1.

代码文件 GCLogAnalysis.java

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

public class GCLogAnalysis {
    private static Random random = new Random();
    public static void main(String[] args) {
        // 当前毫秒时间戳
        long startMillis = System.currentTimeMillis();
        // 持续运行毫秒数; 可根据需要进行修改
        long timeoutMillis = TimeUnit.SECONDS.toMillis(1);
        // 结束时间戳
        long endMillis = startMillis + timeoutMillis;
        LongAdder counter = new LongAdder();
        System.out.println("正在执行...");
        // 缓存一部分对象; 进入老年代
        int cacheSize = 2000;
        Object[] cachedGarbage = new Object[cacheSize];
        // 在此时间范围内,持续循环
        while (System.currentTimeMillis() < endMillis) {
            // 生成垃圾对象
            Object garbage = generateGarbage(100*1024);
            counter.increment();
            int randomIndex = random.nextInt(2 * cacheSize);
            if (randomIndex < cacheSize) {
                cachedGarbage[randomIndex] = garbage;
            }
        }
        System.out.println("执行结束!共生成对象次数:" + counter.longValue());
    }

    // 生成对象
    private static Object generateGarbage(int max) {
        int randomSize = random.nextInt(max);
        int type = randomSize % 4;
        Object result = null;
        switch (type) {
            case 0:
                result = new int[randomSize];
                break;
            case 1:
                result = new byte[randomSize];
                break;
            case 2:
                result = new double[randomSize];
                break;
            default:
                StringBuilder builder = new StringBuilder();
                String randomString = "randomString-Anything";
                while (builder.length() < randomSize) {
                    builder.append(randomString);
                    builder.append(max);
                    builder.append(randomSize);
                }
                result = builder.toString();
                break;
        }
        return result;
    }
}

首先在terminal中编译一下此文件

javac GCLogAnalysis.java

如果有乱码报错无法编译,可以加字符集参数

javac -encoding UTF-8 GCLogAnalysis.java

编译完成之后会生成一个 GCLogAnalysis.class文件

执行 命令:

$ java -XX:+PrintGCDetails GCLogAnalysis
$ java -XX:+PrintGCDetails GCLogAnalysis
正在执行...
[GC (Allocation Failure) [PSYoungGen: 49139K->8189K(57344K)] 49139K->16650K(188416K), 0.0070231 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 57305K->8176K(106496K)] 65766K->34377K(237568K), 0.0159032 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 106480K->8190K(106496K)] 132681K->69927K(237568K), 0.0229538 secs] [Times: user=0.03 sys=0.01, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 106020K->8178K(204800K)] 167757K->103995K(335872K), 0.0203049 secs] [Times: user=0.00 sys=0.03, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 8178K->0K(204800K)] [ParOldGen: 95817K->97054K(201728K)] 103995K->97054K(406528K), [metaspace: 2678K->2678K(1056768K)], 0.0266877 secs] [Times: user=0.06 sys=0.00, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 196608K->8189K(204800K)] 293662K->156047K(406528K), 0.0251842 secs] [Times: user=0.08 sys=0.05, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 8189K->0K(204800K)] [ParOldGen: 147858K->138866K(278016K)] 156047K->138866K(482816K), [metaspace: 2678K->2678K(1056768K)], 0.0366620 secs] [Times: user=0.11 sys=0.00, real=0.04 secs]
[GC (Allocation Failure) [PSYoungGen: 196420K->62379K(387072K)] 335287K->201245K(665088K), 0.0281210 secs] [Times: user=0.02 sys=0.08, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 386987K->77822K(408064K)] 525853K->284071K(686080K), 0.0570028 secs] [Times: user=0.03 sys=0.13, real=0.06 secs]
[Full GC (Ergonomics) [PSYoungGen: 77822K->0K(408064K)] [ParOldGen: 206249K->230046K(398848K)] 284071K->230046K(806912K), [metaspace: 2678K->2678K(1056768K)], 0.0563328 secs] [Times: user=0.11 sys=0.05, real=0.06 secs]
[GC (Allocation Failure) [PSYoungGen: 330240K->102088K(648704K)] 560286K->332134K(1047552K), 0.0484569 secs] [Times: user=0.06 sys=0.13, real=0.05 secs]
执行结束!共生成对象次数:5949
Heap
 PSYoungGen      total 648704K, used 361282K [0x0000000780900000, 0x00000007b2500000, 0x00000007c0000000)
  eden space 524288K, 49% used [0x0000000780900000,0x000000079061e6b0,0x00000007a0900000)
  from space 124416K, 82% used [0x00000007a8e80000,0x00000007af232278,0x00000007b0800000)
  to   space 136704K, 0% used [0x00000007a0900000,0x00000007a0900000,0x00000007a8e80000)
 ParOldGen       total 398848K, used 230046K [0x0000000701a00000, 0x0000000719f80000, 0x0000000780900000)
  object space 398848K, 57% used [0x0000000701a00000,0x000000070faa7940,0x0000000719f80000)
 metaspace       used 2684K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 287K, capacity 386K, committed 512K, reserved 1048576K

 从输出的内容中可以看到GC的次数和类型等详细信息。

 新版本的java有些参数废弃了,见注:

注:
一些常见的即将废弃的jvm参数说明
转自https://python.iitter.com/other/114258.html

jdk1.8.0_171,在应用启动的时候,配置如下jvm参数,会出现警告信息

1. -XX:+UseFastAccessorMethods

含义:get,set 方法转成本地代码(对于jvm来说是冗余代码,jvm将进行优化)
说明: UseFastAccessorMethods option is harmful and has been removed in JDK 9. You need to delete this option from run configuration.

2. -XX:+UseConcMarkSweepGC

含义: 使用CMS GC,默认新生代会使用ParNew
说明:Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

3.-XX:+PrintGCDetails

含义:GC回收信息详解
说明:-XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead.,即需要配置成这样:-Xlog:gc:gc.log

4.-XX:+PrintGCDateStamps

含义:把gc日志带上时间戳
说明:Unrecognized VM option ‘PrintGCDateStamps’

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号