JVM virtual machine --- (4) memory allocation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq1021979964/article/details/96100706

Memory Allocation

Before JDK1.7, JVM memory is divided into three areas: the new generation, old time, permanent generations.

A new generation (Young Generation)

       New Generation goal is to collect as fast as those of the short life cycle of an object, under normal circumstances, all newly generated objects are on the first of the new generation.

manual:

  1. When the Eden area is full, it will trigger young gc, copy the live objects to the From area, when the Eden area again trigger young gc, scans the Eden area and From area, garbage collection, copying surviving objects to the To field, and the Eden area and Fron area emptied.
  2. When Eden and the subsequent trigger young gc, will be Eden and To fields garbage collection, copying surviving objects to the From area and the Eden area and Tao area emptied.
  3. Survival of the object will always repeat the copy in the From and To, after an object copy up to 15 times, is still alive, will be credited to old era.

Note: If you live objects more, To the memory area is not enough storage, it will help space the old era.

The new generation of trigger GC is called MinorGC, subject survival time, High frequency.

New generation memory is divided into: Eden (Yi Paradise ), 2 Survivor Space (survival zone), it is the ratio: 8: 1: 1.

设置-XX:MaxTenuringThreashold参数,可以配置From与To的交换次数。默认15.

设置-XX:SerivorRation参数,可以配置Eden和Servivor比例。默认8.

 

Second, the old year (Old Generation)

Old's store are some of the longer life cycle of an object, usually after multiple garbage collection still surviving in the new generation of objects into the old era.

GC old's trigger is called Full GC, objects survive for a long time, occur less frequently.

Set -XX: pertenureSizeThreashold parameter can be greater than the value of the object directly into the older years without a new generation.

 

Third, the permanent-generation (Permanent Generation)

       Mainly used for storage of permanent generation of static files, such as Java classes and methods. Permanent generation of no significant impact on garbage collection, but some applications may be dynamically generated, or call some Class, for example using reflection, dynamic proxy, when CGLib and other bytecode framework, then you need to set a relatively large permanent generation of space to store the course of these running new increase class.

Note: JDK1.8 when the permanent generation of waste, as java provides permanent to like called "meta-space" technology.

Fourth, on behalf of the cause of permanent waste

       Since the permanent generation of memory often enough, or a memory leak, throw an exception OutOfMemoryError. Yuan essence of space and permanent generation of similar, but the biggest difference between the yuan and the space in the permanent generation of: element space is not a virtual machine, but in the local memory, which is not limited to the JVM memory system can be used, in theory depends 32-bit / 64-bit virtual memory system size.

V. code demonstrates

package com.kevin.jvm.memory;



/**

 * @author caonanqing

 * @version 1.0

 * @description     演示内存分配

 *

 *      设置参数:

 *          -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC -Xms20M -Xmx20M -Xmn10M

 * @createDate 2019/7/13

 */

public class MemoryTest {



    public static void main(String[] args) {



        byte[] b1 = new byte[2*1024*1024];

        byte[] b2 = new byte[2*1024*1024];

        byte[] b3 = new byte[2*1024*1024];

        byte[] b4 = new byte[4*1024*1024];



        System.gc();

    }

}

Sixth, memory allocation parameter options

  • Heap memory parameters

-Xms:表示java虚拟机堆区内存初始内存分配的大小,通常为操作系统可用内存的1/64大小即可
-Xmx:表示java虚拟机堆区内存可被分配的最大上限,通常为操作系统可用内存的1/4大小。
-XX:MinHeapFreeRatio:设置堆空间的最小空间比例。当堆空间的空闲内存小于这个数值时,jvm便会扩展堆空间。
-XX:MaxHeapFreeRatio:设置堆空间的最大空间比例。当堆空间的空闲内存大于这个数值时,jvm便会缩小堆空间。
-XX:NewSize : 设置新生代的大小
-XX:NewRatio:设置老年代与新生代的比例,即老年代除以新生代大小
-XX:SurviorRatio:新生代中eden区与survivior 区的比例
-XX:TargetSurvivorRatio:设置survivior 的使用率。当达到这个空间使用率时,会将对象送入老年代。
-Xmn:该参数是对-XX:newSize、-XX:MaxnewSize两个参数的同时配置,也就是说如果通过-Xmn来配置新生代的内存大小,那么-XX:newSize = -XX:MaxnewSize = -Xmn。

However, the development process, the same configuration will usually -Xms -Xmx two parameter values, which are designed to be able to completely clean the waste of resources need to re-partition the heap area size calculation in the heap region java garbage collection.

(If the initial heap memory is full, the GC will try to, if the GC can not get enough memory, it will expand the heap memory, then, if -Xmx is set too small, then expand the heap fails, will throw OutOfMemoryError error ).

Usually just above -Xms development and -Xmx two parameters of the rational allocation.

  • District method parameter configuration

-XX:PermSize:表示非堆区初始内存分配大小,其缩写为permanent size(持久化内存)
-XX:MaxPermSize:表示对非堆区分配的内存的最大上限
  • Stack Capacity Configuration

-Xss:设置线程栈的大小
  • Direct memory configuration native

-XX:MaxDirectMemorySize :指定本机直接内存大小,如果不指定就表示和Java堆最大值(-Xmx)相同。

 

Guess you like

Origin blog.csdn.net/qq1021979964/article/details/96100706