BAT p3 straight series: thorough explanation of JVM JVM memory model and configuration parameters in detail

VM basic content of BAT is a compulsory interview, and today we open Detailed entire series start JVM JVM memory model, I hope after reading the entire series, you can easily through the BAT assessment on the JVM.

BAT compulsory JVM series of special

1.JVM memory model

2.JVM garbage collection algorithm

3.JVM garbage collector

4.JVM arguments detailed

5.JVM Performance Tuning

JVM memory structure

From the above chart we can clearly see the JVM memory space is divided into three parts:

  1.  Heap
  2.  Methods district
  3.  Stack memory

Stack memory which can be subdivided into java virtual machine stack and native method stacks, heap memory can be divided into the old and the new generation's, the new generation can also be subdivided into Eden area, From Survivor To Survivor district and region.

Some of which are shared by the threads, including Java heap and method area; the other part is the thread private, including virtual machine stack and native method stacks, and program counter this small part of memory.

Heap (Heap)

 

java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. The heap is shared by all threads in the region, it is created when the virtual machine starts. The heap is an object instance stored (new objects exist out of the heap).

The sole purpose of this memory area is stored object instance (new objects), almost all object instances are here to allocate memory.

 

Heap memory is divided into two parts: the old and the young generation's. We usually say garbage collection, recycling is the main heap. Finer division that the new generation can be divided into two areas and Eden Survivor areas (Survivor the From and To Survivor).

The figure is representative of the permanent generation of Perm, but note is not part of the permanent generation of heap memory, it has been permanently removed after the generation of simultaneously jdk1.8.

2: 1 ratio of the value of the new generation (Young) years old (Old) (the value parameter may -XX: NewRatio specified)

Default, Eden: from: to = 8: 1: 1 (by parameter -XX: setting SurvivorRatio), namely: Eden = 8/10 new generation space, from = to = 1/10 new generation size of space.

Method region (Method Area)

District method, also known as "permanent generation", which is used to store the virtual machine to load class information, constants, static variables, each thread is a shared memory area.

In the HotSpot JVM before JDK8, store these "permanent" in the area called "permanent-generation (permanent generation)". Generation of a continuous permanent heap space, before the JVM startup by setting parameters in the command line -XX: MaxPermSize permanent set can be allocated on behalf of the maximum memory space, the default size is 64M (64 bit JVM default 85M).

With the arrival of JDK8, JVM is no longer a permanent generations (PermGen). But the class of metadata information (metadata) still, but no longer stored on contiguous heap space, but to move to is called "Metaspace" local memory (Native memory.

The method of generation of district or immortalized settings

  •  -XX: PermSize = 64MB minimum size, the initial allocation
  •  -XX: MaxPermSize = 256MB maximum allowable size distribution, according to need
  •  XX: + CMSClassUnloadingEnabled -XX: + CMSPermGenSweepingEnabled not set garbage recycling
  •  The default size
  •  MaxPermSize default under the -server option of 64m
  •  By default -client option MaxPermSize to 32m

VM stack (JVM Stack)

 

java virtual machine thread stack is a private, life cycle and the same thread. Thread creation time will create a java virtual machine stack.

Java virtual machine execution time of the program, each method creates a stack frame, stack frames stored in the java virtual machine stack, call stack by way of a push method.

 

At stack frame is divided into several areas: local variable table, the operand stack, the dynamic connection, the method exports.
Usually we call stack variables exist, this saying is not rigorous, it should be said that local variables stored in the local variable table in java virtual machine stack.
local variables of the basic type 8 java local variable table stored in the stack in a virtual machine, if the variable is a reference type, only the reference to address storage objects.

 

 

 

Native method stacks (Native Stack)

Native method stacks (Native Method Stacks) and the role played by the virtual machine stack is very similar, but the difference is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and is for the native method stacks virtual machine to use a method of Native service.

Program Counter (PC Register)

 

Recording position of the program counter is the current thread executing the program, changes the value of the counter to determine the next instruction to be executed, such as looping, branching, jump method, exception handling, are dependent on the program counter thread resumes to complete.
Multi-threaded Java Virtual Machine is in turn switched and allocation of processor execution time achieved by a thread. To thread switches can be restored to the correct position, each thread requires a separate program counter, so it is thread-private.

 

 

Direct Memory

Direct Memory is not part of the virtual machine memory, nor memory region Java Virtual Machine Specification defined. jdk1.4 the NIO newly added, and the introduction of the IO buffer mode channel, it can directly call the method allocates heap Native outer memory, the external memory is native heap memory, will not affect the size of the heap memory.

JVM memory parameters

  •  -Xms set minimum heap space.
  •  -Xmx sets the maximum size of the heap space.
  •  -Xmn: Set the size of the young generation
  •  -XX: NewSize set up the new generation of the minimum amount of space.
  •  -XX: MaxNewSize set up the new generation of maximum space.
  •  -XX: PermSize substituting permanent settings the minimum of space.
  •  -XX: MaxPermSize set up a permanent space on behalf of maximum size.
  •  -Xss set the stack size for each thread
  •  -XX: + UseParallelGC: garbage collector to select the parallel collector. This configuration is valid only for the young generation. That the above arrangement, the young generation to use concurrent collection, while still using the old generation of the serial collector.
  •  -XX: ParallelGCThreads = 20: number of threads parallel collector configuration, namely: simultaneously with many garbage collection threads. This configuration value is preferably equal to the number of processors.

Typical parameters JVM Reference:

  •  java-Xmx3550m-Xms3550m-Xmn2g-Xss128k
  •  -XX:ParallelGCThreads=20
  •  -XX:+UseConcMarkSweepGC-XX:+UseParNewGC

-Xmx3550m:设置JVM最大可用内存为3550M。

-Xms3550m:设置JVM促使内存为3550m。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存。

-Xmn2g:设置年轻代大小为2G。整个堆大小=年轻代大小+年老代大小+持久代大小。持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,官方推荐配置为整个堆的3/8。

-Xss128k:设置每个线程的堆栈大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。更具应用的线程所需内存大
小进行调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000
左右。

Guess you like

Origin www.cnblogs.com/mxb0611/p/12081728.html