The JVM memory model

Memory model and run-time data area

Key data is stored heap and method area. For virtual machine stack, native method stacks, program counter are thread private.

Divided heap, non-heap area

Reactor divided into two blocks, one old region, a region is Young

Young divided into two blocks, one Survivor (s0 + s1) as large as, and may also be referred From To

 

As shown below:  

 

Creating general objects and arrays will be allocated on the heap memory space, but there are several areas in the heap, specifically assigned to that area?

Area where the object is created

  The new objects are allocated in general to the Eden area, due to the limited memory space Eden area, if memory reaches a critical value, then the time required for Eden memory space to clean up the so-called garbage collection (Garbage Collect), so the GC called Minor GC, Minor GC is GC Young area.

  After GC, some objects will be cleared away, some objects may also alive, you need to copy the object to Survivor areas for its survival, then clean out the object of Eden.

Survivor areas

Survivor areas can be divided into: S0 + S1 (From + To)

At the same time point, S0 or S1 is the presence of only one data region, and the other is an empty.

Analog GC:

  • For example, only this time the Eden area and the area has an object From, To is empty.
  • This time for GC, From the age of objects in the region will be +1, we all know the Eden area live objects are copied to the To field, From zone can survive there will be two objects place.
  • If the target age reaches a critical value previously set, and the object will be moved to the Old District, has not reached the critical value will be copied to the To area
  • From this time Eden district and area has been cleared (by GC objects certainly did not, not have their own place objects GC)
  • This time From and To switch roles before From To become, before To become From
  • That is in any case an area called To ensure Survivor is empty
  • Minor GC will repeat this process, To area is filled, all objects are copied to the old era.

Old District

  General Old Age area is relatively large objects, or exceeds this threshold, there will be a GC operation in the Old District, GC area called Old Major GC.

How to understand the Minor / Major / Full GC

  Minor GC: New Generation

  Major GC: years old

  Full GC: Cenozoic + years old

 Garbage Collect

  For an object, only to be sure it is garbage collected. Java is automatic memory management and garbage collection to do, the automatic garbage collection mechanism is to find the Java heap objects, and object classification and discrimination, looking for objects and object being used is not used, then these objects are not used to clear.

How to determine whether an object is garbage

  • Reference counting

  For an object, just hold the object referenced in the application, it means the object is not garbage, contrary explanation is garbage.

  • Reachability analysis

  By GC Root objects, and start looking ahead to see if an object is reachable.

  (As GC Root: class loader, Thread, local variable table virtual machine stack, static members, constant references to local variables method stack)

  VM stack (local variables table stack frame) referenced objects

  Method static property class object referenced by region

  The method of constant reference object region

  Native method stack JNI (ie, the general said, Native Method) reference objects

Garbage collection algorithm

Clear mark (Mark-Sweep)

  • Tags: find objects in memory need to be recovered, and mark them out

      At this heap all objects will be scanned again, and thus be able to determine the object need to be recovered, time-consuming

  • Clear: removed objects need to be marked recovery, releasing the corresponding memory space

  Disadvantages: Clear labeling will generate a lot of space in the memory space debris, space debris could cause too much time in the future need to allocate large objects in the program is running, can not find enough contiguous memory and had to depart early for another garbage collection action.

      Mark and sweep two processes are time-consuming, inefficient.

      It will produce large amounts of space debris.

Replication (copying)

The memory is divided into two equal regions, each of which only one

 

 Wherein a memory usage when finished, will also copy live objects to another one, then the memory have been used once clear 4 removed.

 

  Disadvantages: low space utilization

      复制收集算法在对象存活率较高时就要进行较多的复制操作,效率会变低。更关键的是,还要浪费50%的空间,就需要有额外的空间进行分配担保,以应对被使用的内存中所有的对象都有100%存活的极端情况,所以老年代一般不能直接选用这种算法。

标记-整理(Mark-Compact)

  标记整理:标记过程任然与“标记-清除”算法一样,但是不是直接将可回收对象进行清理,而是让所有存活的对象都向一端移动,然后直接清理掉端边界以外的内存。

 

让所有的存活对象都向一端移动,清理掉边界以外的存在

 

分代收集算法

在堆内存中使用的算法

Young区:复制算法(对象在被分配之后,可能生命周期比较短,Young区复制效率高)

Old区:标记清除或标记整理(Old区对象存活时间较长,来回复制是没有必要的)

Guess you like

Origin www.cnblogs.com/LBJLAKERS/p/12287322.html