文章标签 » JAVA

Java获取当前系统类型

Java获取当前系统类型完整版

系统枚举类:

public enum EPlatform {
    Any("any"),
    Linux("Linux"),
    Mac_OS("Mac OS"),
    Mac_OS_X("Mac OS X"),
    Windows("Windows"),
    OS2("OS/2"),
    Solaris("Solaris"),
    SunOS("SunOS"),
    MPEiX("MPE/iX"),
    HP_UX("HP-UX"),
    AIX("AIX"),
    OS390("OS/390"),
    FreeBSD("FreeBSD"),
    Irix("Irix"),
    Digital_Unix("Digital Unix"),
    NetWare_411("NetWare"),
    OSF1("OSF1"),
    OpenVMS("OpenVMS"),
    Others("Others");

    private EPlatform(String desc) {
        this.description = desc;
    }

    public String toString() {
        return description;
    }

    private String description;
}

获取系统类型类:

public class OSinfo {

    private static String OS = System.getProperty("os.name").toLowerCase();

    private static OSinfo _instance = new OSinfo();

    private EPlatform platform;

    private OSinfo(){}

    public static boolean isLinux(){
        return OS.indexOf("linux")>=0;
    }

    public static boolean isMacOS(){
        return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")<0;
    }

    public static boolean isMacOSX(){
        return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")>0;
    }

    public static boolean isWindows(){
        return OS.indexOf("windows")>=0;
    }

    public static boolean isOS2(){
        return OS.indexOf("os/2")>=0;
    }

    public static boolean isSolaris(){
        return OS.indexOf("solaris")>=0;
    }

    public static boolean isSunOS(){
        return OS.indexOf("sunos")>=0;
    }

    public static boolean isMPEiX(){
        return OS.indexOf("mpe/ix")>=0;
    }

    public static boolean isHPUX(){
        return OS.indexOf("hp-ux")>=0;
    }

    public static boolean isAix(){
        return OS.indexOf("aix")>=0;
    }

    public static boolean isOS390(){
        return OS.indexOf("os/390")>=0;
    }

    public static boolean isFreeBSD(){
        return OS.indexOf("freebsd")>=0;
    }

    public static boolean isIrix(){
        return OS.indexOf("irix")>=0;
    }

    public static boolean isDigitalUnix(){
        return OS.indexOf("digital")>=0&&OS.indexOf("unix")>0;
    }

    public static boolean isNetWare(){
        return OS.indexOf("netware")>=0;
    }

    public static boolean isOSF1(){
        return OS.indexOf("osf1")>=0;
    }

    public static boolean isOpenVMS(){
        return OS.indexOf("openvms")>=0;
    }

    /**
     * 获取操作系统名字
     * @return 操作系统名
     */
    public static EPlatform getOSname(){
        if(isAix()){
            _instance.platform = EPlatform.AIX;
        }else if (isDigitalUnix()) {
            _instance.platform = EPlatform.Digital_Unix;
        }else if (isFreeBSD()) {
            _instance.platform = EPlatform.FreeBSD;
        }else if (isHPUX()) {
            _instance.platform = EPlatform.HP_UX;
        }else if (isIrix()) {
            _instance.platform = EPlatform.Irix;
        }else if (isLinux()) {
            _instance.platform = EPlatform.Linux;
        }else if (isMacOS()) {
            _instance.platform = EPlatform.Mac_OS;
        }else if (isMacOSX()) {
            _instance.platform = EPlatform.Mac_OS_X;
        }else if (isMPEiX()) {
            _instance.platform = EPlatform.MPEiX;
        }else if (isNetWare()) {
            _instance.platform = EPlatform.NetWare_411;
        }else if (isOpenVMS()) {
            _instance.platform = EPlatform.OpenVMS;
        }else if (isOS2()) {
            _instance.platform = EPlatform.OS2;
        }else if (isOS390()) {
            _instance.platform = EPlatform.OS390;
        }else if (isOSF1()) {
            _instance.platform = EPlatform.OSF1;
        }else if (isSolaris()) {
            _instance.platform = EPlatform.Solaris;
        }else if (isSunOS()) {
            _instance.platform = EPlatform.SunOS;
        }else if (isWindows()) {
            _instance.platform = EPlatform.Windows;
        }else{
            _instance.platform = EPlatform.Others;
        }
        return _instance.platform;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(OSinfo.getOSname());// 获取系统类型
        System.out.println(OSinfo.isWindows());// 判断是否为windows系统 
    }

}

测试结果:

在这里插入图片描述

转载自:https://blog.csdn.net/fangchao2011/article/details/88785637

如何阅读GC日志

Demo

without evacuation (partially stw)
1.416: [GC pause (young) (initial-mark), 0.62417980 secs]                    // initial mark phase…....2.042: [GC concurrent-root-region-scan-start]                                // concurrent scan phase start2.067: [GC concurrent-root-region-scan-end, 0.0251507]                       // concurrent scan phase end2.068: [GC concurrent-mark-start]                                            // concurrent mark phase start3.198: [GC concurrent-mark-reset-for-overflow]                               //   well, some awkward exception occurred and would be self-adjusted soon4.053: [GC concurrent-mark-end, 1.9849672 sec]                               // concurrent mark phase end4.055: [GC remark 4.055: [GC ref-proc, 0.0000254 secs], 0.0030184 secs]      // remark phase done, stw[Times: user=0.00 sys=0.00, real=0.00 secs]                           4.088: [GC cleanup 117M->106M(138M), 0.0015198 secs]                         // cleanup phase done, stw[Times: user=0.00 sys=0.00, real=0.00 secs]4.090: [GC concurrent-cleanup-start]                                         // concurrently free up regions that is empty, start4.091: [GC concurrent-cleanup-end, 0.0002721]                                // concurrently free up regions that is empty, end

with evacuation (completely stw)
0.522: [GC pause (young), 0.15877971 secs]                                     // 这是一次ygc// 开始时间点0.522,stw持续时间0.15877971[Parallel Time: 157.1 ms]                                                   //   本次ygc的并行时间157.1ms[GC Worker Start (ms):  522.1  522.2  522.2  522.2                       //     4个GCWorker的启动时间Avg: 522.2, Min: 522.1, Max: 522.2, Diff:   0.1]                        //       平均值,最小值,最大值,最大最小差[Ext Root Scanning (ms):  1.6  1.5  1.6  1.9                             //     4个GCWorker扫描根的时长Avg:   1.7, Min:   1.5, Max:   1.9, Diff:   0.4]                        //       平均值,最小值,最大值,最大最小差[Update RS (ms):  38.7  38.8  50.6  37.3                                 //     4个GCWorkrer更新RS的时长Avg:  41.3, Min:  37.3, Max:  50.6, Diff:  13.3]                        //       平均值,最小值,最大值,最大最小差[Processed Buffers : 2 2 3 2                                          //     4个GCWorker更新的RS数量Sum: 9, Avg: 2, Min: 2, Max: 3, Diff: 1]                             //       总数,平均值,最小值,最大值,最大最小差[Scan RS (ms):  9.9  9.7  0.0  9.7                                       //     4个GCWorker扫描RS的时长Avg:   7.3, Min:   0.0, Max:   9.9, Diff:   9.9]                        //       平均值,最小值,最大值,最大最小差[Object Copy (ms):  106.7  106.8  104.6  107.9                           //     4个GCWorker复制存活对象的时长Avg: 106.5, Min: 104.6, Max: 107.9, Diff:   3.3]                        //       平均值,最小值,最大值,最大最小差[Termination (ms):  0.0  0.0  0.0  0.0                                   //     4个GCWorker的终止时长Avg:   0.0, Min:   0.0, Max:   0.0, Diff:   0.0]                        //       平均值,最小值,最大值,最大最小差[Termination Attempts : 1 4 4 6                                       //     4个GCWorker的尝试终止次数Sum: 15, Avg: 3, Min: 1, Max: 6, Diff: 5]                            //       总数,平均值,最小值,最大值,最大最小差[GC Worker End (ms):  679.1  679.1  679.1  679.1                         //     4个GCWorker的停止时间Avg: 679.1, Min: 679.1, Max: 679.1, Diff:   0.1]                        //       平均值,最小值,最大值,最大最小差[GC Worker (ms):  156.9  157.0  156.9  156.9                             //     4个GCWorker的存活时间Avg: 156.9, Min: 156.9, Max: 157.0, Diff:   0.1]                        //       平均值,最小值,最大值,最大最小差[GC Worker Other (ms):  0.3  0.3  0.3  0.3                               //     4个GCWorker花费在其他杂事上的时间Avg:   0.3, Min:   0.3, Max:   0.3, Diff:   0.0]                        //       平均值,最小值,最大值,最大最小差[Clear CT:   0.1 ms]                                                        //   清除卡表的时长,本阶段串行执行[Other:   1.5 ms]                                                           //   花费在其他时间上事情上的时间,本阶段串行执行[Choose CSet:   0.0 ms]                                                  //     为回收集选择区域的时间[Ref Proc:   0.3 ms]                                                     //     处理对象引用的时间[Ref Enq:   0.0 ms]                                                      //     对象引用入队时间[Free CSet:   0.3 ms]                                                    //     释放回收集的时间[Eden: 12M(12M)->0B(10M) Survivors: 0B->2048K Heap: 13M(64M)->9739K(64M)]   //   回收效果//   Eden从12M回收到0B,容量从12M减少到10M//   Survivors从0B增加到2048K//   堆从13M减少到9739K,容量保持不变[Times: user=0.59 sys=0.02, real=0.16 secs]

参考资料

权威参考
https://blogs.oracle.com/poonam/understanding-g1-gc-logs

方法内统计耗时的几种方法

1、自己写,计时开始结束使用System.currentTimeMillis()

long start = System.currentTimeMillis();
TimeUnit.SECONDS.sleep(3L);
System.out.println(“method finish , use time ” +(System.currentTimeMillis() – start) + “ms”);

2、使用StopWatch类来实现

StopWatch类有不同的实现,spring有一个,guava也有一个,具体使用根据自己项目的依赖情况。真正使用上基本一致。

使用方式:

System.out.println(“SLAMonitorThread.main() start”);
StopWatch sw = new StopWatch();
sw.start();
TimeUnit.SECONDS.sleep(1L);
sw.split();
System.out.println(
“SLAMonitorThread.main() end. split:” + sw.getSplitTime() + “, ” + sw.toSplitString());
TimeUnit.SECONDS.sleep(1L);
sw.split();
System.out.println(
“SLAMonitorThread.main() end. split:” + sw.getSplitTime() + “, ” + sw.toSplitString());
TimeUnit.SECONDS.sleep(1L);
sw.split();
System.out.println(
“SLAMonitorThread.main() end. split:” + sw.getSplitTime() + “, ” + sw.toSplitString());
TimeUnit.SECONDS.sleep(1L);
System.out.println(“SLAMonitorThread.main() end. end:” + sw.getTime() + “, ” + sw.toString());
long start = System.currentTimeMillis();
TimeUnit.SECONDS.sleep(1L);

两种方式的优缺点:
方式1是不用任何外部依赖就可以实现的,但是缺点也同样明显,每个位置要打印一次的时候,都需要自己计算,并且还要重新拿到开始时间。
方式2则正好相反,需要guava的依赖,但是好处是提供了比较常用的接口支持,在不同的位置统计,并且计算一些东西,比如某一段耗时占比之类的,都可以友好支持。

具体使用时根据自己情况即可。