Stack and heap tuning parameters

A, and then substituting permanent proposed method area

  Previous mentioned: 8 in the previous version, the JVM uses a portion of the heap implementation area, this part of the stack space is called "permanent generation", since the structure and operation of the information classes is on the method constants pool area, permanent implementation on behalf of district easily lead to heap overflow. After the launch of version 8, Java uses a heap memory that is outside the native area of ​​physical memory implementation, we put some space called "meta-space."

  

 

  

 

Second, the heap memory

  As we all know, when the thread is new an object, object reference variables stored on the stack needle, and the object itself is stored in the heap memory, if a large number of objects stored in the heap memory and can not be recovered GC, it will lead to OOM error, heap space be? GC workflow is like? You can set the size of the heap space it? GC can choose the way you work?

  1, GC heap memory structure and procedure:

  

 

 

   New Generation Space: Space Year Old = 1: 3, and wherein the new generation of Eden: from: to Area = 8: 1: 1

  The new generation is born instance of the object region, but also the vast majority of instances of objects demise of the area, the new generation is divided into two areas: Eden District area and Survivor, Survivor SurvivorFrom area is divided into zones and SurvivorTo area. All objects are out in the new Eden area. When the Eden area is full will trigger the first GC, the surviving object will be "copied" to the From area. If the Eden area again this trigger GC, GC at this time will sweep the surface area and the Eden From area, garbage collection between the two areas, after this recovery still live objects are copied directly into the To field, and these objects "age" +1. 

  Upon completion of copying, empty objects Eden and From area. After completion of the emptying, the From and To go to swap, the area is empty that region as the To field, the size of these two regions is the same. After the exchange is completed, the next trigger GC continue to scan Eden and From area, and once again surviving objects into the To field, and emptied From Eden area, then swap From and To, who made empty To whom, for the next garbage collection scans do ready. When an object is copied back and forth From To Zone area, "age" can not always be growing back, when the "age" is the default value MaxTenuringThreshold reach 15, this object will enter the old era.

  2, the heap memory tuning

  Java虚拟机在启动时,初始内存为本机内存的1/64,当此内存不够实用,虚拟机会尝试获取更多的内存空间,但是默认情况下最大可使用内存为本机内存大额1/4。当然你可以通过配置虚拟机参数来改变虚拟机占用内存的情况,在生产环境中,建议配置初始内存空间大小和最大可使用空间大小相同,以避免进程争夺内存资源时造成的卡顿。

  可以使用如下代码查看虚拟机占用空间的大小:

public class Demo1 {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());//查看逻辑处理器

        long maxMemory = Runtime.getRuntime().maxMemory();//返回Java虚拟机可以使用的最大内存量
        long totalMemory = Runtime.getRuntime().totalMemory();//返回Java虚拟机中的内存总量

        System.out.println("-Xmx: MAX_MEMORY = " + maxMemory + "(字节)、" + (maxMemory / (double)1024/1024) + "MB");
        System.out.println("-Xms: TOTAL_MEMORY = " + totalMemory + "(字节)、" + (totalMemory / (double)1024/1024) + "MB");
    }
}
4
-Xmx: MAX_MEMORY = 1886912512(字节)、1799.5MB
-Xms: TOTAL_MEMORY = 128974848(字节)、123.0MB

  3、配置虚拟机占用内存大小

  IDEA:

  

  STS

  

 

 

   参数:

  -Xms:设置初始(start)分配内存大小  -Xmx:设置最大(max)分配内存大小  -XX:+PrintGCDetails:输出详细的GC处理日志

  4、制造错误、日志分析

  为了更快地导致内存溢出,我将-Xms和-Xmx都设置成10m。

public class Person {
    private String name;
    byte[] bytes = new byte[10*1024*1024];
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class Demo1 {
    public static void main(String[] args) {
        while(true){
            Person person = new Person(UUID.randomUUID().toString());
        }
    }
}
[GC (Allocation Failure) [PSYoungGen: 2048K->504K(2560K)] 2048K->696K(9728K), 0.0032340 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 1289K->504K(2560K)] 1481K->812K(9728K), 0.0006781 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 504K->488K(2560K)] 812K->852K(9728K), 0.0006038 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 364K->774K(7168K)] 852K->774K(9728K), [Metaspace: 4020K->4020K(1056768K)], 0.0075233 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 774K->774K(9728K), 0.0003232 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 774K->753K(7168K)] 774K->753K(9728K), [Metaspace: 4020K->4020K(1056768K)], 0.0113270 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.qlu.juc.Person.<init>(Person.java:5)
    at com.qlu.juc.Demo1.main(Demo1.java:8)
Heap
 PSYoungGen      total 2560K, used 99K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 4% used [0x00000000ffd00000,0x00000000ffd18d90,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 753K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 10% used [0x00000000ff600000,0x00000000ff6bc510,0x00000000ffd00000)
 Metaspace       used 4051K, capacity 4572K, committed 4864K, reserved 1056768K
  class space    used 454K, capacity 460K, committed 512K, reserved 1048576K

  ①

 

 

 

 在分配内存是,我指定了虚拟机最大占用内存为10m,而此处仅新生代和老年代就占用了近乎10m,足以证明元空间并不使用虚拟机内存实现,而是使用了本地内存。

  ②分析日志:内存占用

 

 

   ③分析日志:GC日志

[GC (Allocation Failure) [PSYoungGen: 1289K->504K(2560K)] 1481K->812K(9728K), 0.0006781 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

[GC (Allocation Failure)
指出了GC类型,GC负责收集新生代的Eden和From区域,Allocation Failure表示空间分配失败
[PSYoungGen: 1289K->504K(2560K)]
1289k表示YoungGC收集前新生代内存占用情况,504k表示YoungGC后新生代内存占用情况,此轮GC释放了785k内存。(2560)表示新生代总占用空间大小
1481K->812K(9728K), 0.0006781 secs]
1481k表示YoungGC收集前堆的使用情况,812k表示YoungGC收集之后堆空间的使用情况,次轮GC释放了669k内存。(9728k)表示堆空间的总大小,还有10m-9728k被栈、程序计数器、本地方法栈占用。
0.0006781secs表示YoungGC耗时。
[Times: user=0.00 sys=0.00, real=0.00 secs]
user:YoungGC用户耗时,sys:YoungGC 系统耗时,real:YoungGC实际耗时

[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 774K->753K(7168K)] 774K->753K(9728K), [Metaspace: 4020K->4020K(1056768K)], 0.0113270 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 

[Full GC (Allocation Failure)
指出了GC类型,Full GC负责收集老年代区域,Allocation Failure表示空间分配失败
[PSYoungGen: 0K->0K(2560K)]
新生代:GC前后Young区内存占用情况
[ParOldGen: 774K->753K(7168K)]
老年代:GC前老年代的占用情况,可以看出基本没有回收,7168k表示老年代内存空间大小
774K->753K(9728K),
GC前后堆的占用情况以及对的总内存大小
[Metaspace: 4020K->4020K(1056768K)], 0.0113270 secs]
元空间占用情况和元空间大小,Full GC耗时
[Times: user=0.02 sys=0.00, real=0.01 secs]
user:YoungGC用户耗时,sys:YoungGC 系统耗时,real:YoungGC实际耗时。

  当老年代空间执行了Full GC后发现依然无法进行新对象的保存(空间分配)即Allocation Failure,就会报出“OutOfMemoryError”。

Guess you like

Origin www.cnblogs.com/superlsj/p/11671675.html