深刻理解jvm—分析

    java开发过程内存报错是常见bug,围绕内存问题,今天参考周志明的深刻理解java虚拟机理解。php

如图,共享数据区包括 方法区和堆;隔离数据区包括虚拟堆栈和本地方法栈,程序计数器。java

 

  1. 程序计数器:线程私有,记录当前线程所执行的字节码的行号指示器,任何一个时刻一个内核都只会执行一条线程中的指令;且此内存区域是为一个在java虚拟机 中没有规定任何OUTOFMEMEORYERROR的区域。
  2. java栈:线程私有,生命周期与线程相同。记录局部变量,类型有各类基础数据类型,对象引用。其中包括两种异常 StackOverflowError:线程请求的栈深度大于虚拟机所容许的深度;OutOfMemoryError:可动态扩展的虚拟机栈没法申请到足够的内存。
  3. 本地方法栈:和虚拟机栈相似,服务与Native方法服务。
  4. java堆:存放对象实例。垃圾收集器管理的主要区域。经过参数-Xmx,-Xms扩展,没法扩展时提示OutOfMemoryError异常。
  5. 方法区:各个线程的共享内存区域,用于存储已被虚拟机加载的类信息,静态变量,常量,即便编译后的代码等数据。此区域对内存限制较为宽松,但一样会抛出OutOfMemoryError异常。

  注意:栈记录的是局部变量,类中的int 等,方法区记录的是静态变量: static int.两者此处不一样区域。eclipse

java堆溢出:spa

/**
 * VM Args:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError

vm参数是传给虚拟机的,程序参数是传给main;
 */
public class HeapOOM {

    static class OOMObject {
    }

    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<OOMObject>();

        while (true) {
            list.add(new OOMObject());
        }
    }
}

效果线程

[GC (Allocation Failure) [PSYoungGen: 7716K->1016K(9216K)] 7716K->5225K(19456K), 0.0114724 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] 
[GC (Allocation Failure) --[PSYoungGen: 9208K->9208K(9216K)] 13417K->19440K(19456K), 0.0214058 secs] [Times: user=0.08 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 9208K->0K(9216K)] [ParOldGen: 10232K->9865K(10240K)] 19440K->9865K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4930188 secs] [Times: user=0.52 sys=0.00, real=0.49 secs] 
[Full GC (Ergonomics) [PSYoungGen: 7630K->8076K(9216K)] [ParOldGen: 9865K->7755K(10240K)] 17495K->15832K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4078616 secs] [Times: user=0.41 sys=0.00, real=0.41 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 8076K->8074K(9216K)] [ParOldGen: 7755K->7755K(10240K)] 15832K->15829K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4094363 secs] [Times: user=0.44 sys=0.00, real=0.41 secs] 
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid6376.hprof ...
Heap dump file created [27904125 bytes in 0.355 secs]
[Full GC (Ergonomics) [PSYoungGen: 8192K->0K(9216K)] [ParOldGen: 7755K->485K(10240K)] 15947K->485K(19456K), [Metaspace: 2552K->2552K(1056768K)], 0.0377330 secs] [Times: user=0.02 sys=0.00, real=0.04 secs] 
Heap
 PSYoungGen      total 9216K, used 82K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 1% used [0x00000000ff600000,0x00000000ff614938,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 10240K, used 485K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 4% used [0x00000000fec00000,0x00000000fec79428,0x00000000ff600000)
 Metaspace       used 2558K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 274K, capacity 386K, committed 512K, reserved 1048576K
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:261)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
	at java.util.ArrayList.add(ArrayList.java:458)
	at Test.main(Test.java:14)

Memory Analyzer 分析内存:code

下载http://www.eclipse.org/mat/downloads.php对象

载入dump文件,生命周期

 

查看java内存映像:进程

1,用jps查看java运行进程的lvmid;ip

2,jmap生成dump文件;

3,jhat查看dump文件

相关文章
相关标签/搜索