优化及排查

服务端性能问题排查及优化 ---内存问题分析

2019-02-18  本文已影响15人  土司阿哈

概述

内存问题主要体现在以下几个方面

本文主要介绍内存溢出的排查方法。

高并发的系统中,服务端响应速度对服务本身影响很大,响应时间从1秒增加到2秒会导致内存里的对象的生命周期变长,导致内存增大。生命周期变长还会导致原本在Young区能回收的对象提升到Old区,导致频繁的FGC。

CMS方式可以减少停顿时间,但并不是没有,内存分配不合理还是会导致一些问题,比如业务场景导致大部分对象会进入Old区,Old区分配比较大,频繁的FGC也比较浪费资源,可以考虑重新分配Young区、Old区大小以及Eden:S0:S1比例,使对象尽量的在Young区回收。

可能导致内存溢出的情况

分析方法

首先保证出现问题的时候有东西去分析,可通过以下方法去保留事发现场。

分析dump信息

内存泄漏示例

Demo 代码

MemoryLeak

public class MemoryLeak {
    private long id;
    private Byte[] bytes;

    public MemoryLeak() {
        id = new Date().getTime();
        int count = 256;
        bytes = new Byte[count];
        for (int i = 0; i < count; i++) {
            bytes[i] = (byte) i;
        }
    }
}

TestDemo

package demo.memory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.RuntimeMXBean;
import java.util.List;

/**
 * VM options -Xms128m -Xmx128m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=outofmemory.bin
 *
 * Exception in thread "main" *** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message can't create byte arrau at JPLISAgent.c line: 813
 * java.lang.OutOfMemoryError: Java heap space
 *  at demo.memory.MemoryLeak.<init>(MemoryLeak.java:22)
 *  at demo.memory.TestDemo.main(TestDemo.java:44)
 *
 */
public class TestDemo {
    static MemoryLeak[] memoryLeakArray;
    private static int processId = 0;

    public static void main(String[] args) throws Exception {
        processId = getProcessID();

        MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
        MemoryUsage usage = memorymbean.getHeapMemoryUsage();
        System.out.println("INIT HEAP: " + usage.getInit());
        System.out.println("MAX HEAP: " + usage.getMax());
        System.out.println("USE HEAP: " + usage.getUsed());
        List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
        System.out.println("===================JVM OPTIONS=============== ");
        System.out.println(inputArguments);

        int sec = 30;
        int count = 1024;
        memoryLeakArray = new MemoryLeak[count * sec];
        for (int s = 0; s < sec; s++) {
            for (int i = 0; i < count; i++) {
                memoryLeakArray[s * count + i] = new MemoryLeak();
            }
            Thread.sleep(1000);
            System.out.println("sec:" + s);
            printGcStat();
            //System.in.read();
        }

        while (true) {
            Thread.sleep(1000);
        }

    }

    /**
     * 打印GC性信息
     */
    public static void printGcStat() {
        Process process;
        try {
            process = Runtime.getRuntime().exec("jstat -gcutil " + processId);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取进程ID
     * @return
     */
    public static int getProcessID() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0]);
    }

}

执行结果

demo.memory.TestDemo
INIT HEAP: 134217728
MAX HEAP: 128974848
USE HEAP: 4091336
===================JVM OPTIONS=============== 
[-Xms128m, -Xmx128m, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=outofmemory.bin, -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=57241:/Applications/IntelliJ IDEA.app/Contents/bin, -Dfile.encoding=UTF-8]
sec:0
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  44.05   0.00  17.29  19.76      0    0.000     0    0.000    0.000
sec:1
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  70.05   0.00  17.29  19.76      0    0.000     0    0.000    0.000
sec:2
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  96.06   0.00  17.29  19.76      0    0.000     0    0.000    0.000
sec:3
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.53  21.34  25.99  93.32  83.13      1    0.023     0    0.000    0.023
sec:4
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.53  46.59  25.99  93.32  83.13      1    0.023     0    0.000    0.023
sec:5
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.53  69.88  25.99  93.32  83.13      1    0.023     0    0.000    0.023
sec:6
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.53  95.10  25.99  93.32  83.13      1    0.023     0    0.000    0.023
sec:7
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  21.62  69.21  93.33  83.13      2    0.061     1    0.525    0.586
sec:8
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  47.12  69.21  93.33  83.13      2    0.061     1    0.525    0.586
sec:9
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  72.62  69.21  93.33  83.13      2    0.061     1    0.525    0.586
sec:10
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  98.11  69.21  93.33  83.13      2    0.061     1    0.525    0.586
sec:11
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  38.39  99.82  93.37  83.13      2    0.061     2    0.672    0.733
sec:12
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  63.89  99.82  93.37  83.13      2    0.061     2    0.672    0.733
sec:13
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  89.39  99.82  93.37  83.13      2    0.061     2    0.672    0.733
java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to outofmemory.bin ...
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
Heap dump file created [205085894 bytes in 1.296 secs]
    at demo.memory.MemoryLeak.<init>(MemoryLeak.java:23)
    at demo.memory.TestDemo.main(TestDemo.java:44)

Process finished with exit code 1

JVisualVM分析过程

上一篇 下一篇

猜你喜欢

热点阅读