JVM memory model and GC garbage collection

JVM memory area

1, the program counter

  This is a small memory space, its role can be seen as an indicator byte-code line number of the current thread of execution, referring to the place the last time the code is executed, the thread private.

2, Java Virtual Machine stack

  It is a Java memory model execution method, each method is called to perform the procedure done, it corresponds to a stack frame in a virtual machine stack from stack to stack a process, thread private.

3, native method stacks

  VM stack with a similar, but the native method stacks for executing a native method, thread private.

4, Java heap

  The sole purpose of existence is to store the object in the region, almost all object instances where applications are allocating memory shared by all threads.

5. The method area

  It is used to store class information has been loaded virtual machine, constants, static variables, the time compiler to compile the code and other data, all threads share.

With the end of the thread private area, there is no thread, no garbage collection; gc local operation is shared by all threads in the area.

Generational memory


Why generational?

       Heap memory is the biggest piece of virtual machine memory management, garbage collection is the most frequent an area, we program all object instances are stored in the heap memory. A generational heap memory in order to improve the object memory allocation and garbage collection efficiency. Imagine if the heap memory is not zoning, all newly created objects and object life cycle is very long together, as the program executes, heap memory require frequent garbage collection, and each must be recovered through all objects, traversal of these objects takes time cost is enormous, it will seriously affect our GC efficiency, which is simply terrible.

       With the generational memory, the situation is different, newly created objects will allocate memory in the new generation, after several recovery still surviving objects stored in the old era, static property, class information stored in the permanent generation, the new generation of objects in the survival time is short, only need to GC frequent in the new generation area, old era object life cycle is long, the frequency of garbage collection is relatively low, does not require frequent recycling, recovery of the permanent generation is very poor, generally not garbage collection, it can also use the appropriate garbage collection algorithm based on the characteristics of different ages. Generational collection greatly enhance the collection efficiency, these are the benefits brought about generational memory.

       Memory generational divide

      Java virtual machine heap memory is divided into the new generation, years old and permanent generations, is a unique concept of permanent generation of HotSpot virtual machine, which uses a permanent way to achieve generation method area, no other virtual machine implementations of this concept, and HotSpot there are also canceled permanently on behalf of the trend in the HotSpot JDK 1.7 has already begun "to perpetuate", originally on behalf of the permanent removal of a string constant pool. Permanently store data on behalf of the main constants, class information, such as static variables, little to do with garbage collection, the new generation and the old year is the main area for garbage collection. Memory generational diagram is as follows:

       

       The new generation (Young)

       The new generation of priority objects stored in the new generation, the new generation of students toward the object evening died, the survival rate is very low, in the new generation, conventional applications can be recycled garbage collection generally 70% to 95% of the space, high recovery efficiency.

       The HotSpot new generation is divided into three, a larger and two smaller Eden space Survivor space, the default ratio of 8: 1: 1. HotSpot object divided due to use of the new generation to recover the replication algorithm, the ratio is set to take advantage of memory space, reduce waste. The new generation of object allocations in the Eden area (except for large objects, large objects directly into the old year), when the Eden area there is not enough space allocated, the virtual machine will launch a Minor GC.

       GC starts when the object is present only in the region and Eden From Survivor region, To Survivor empty area (a reserved area). When GC carried out, Eden District, all live objects are copied to the To Survivor region, while in From Survivor region, still live objects will decide fate according to their age values, the age value reaches the age threshold (default is 15, each new generation of objects to get through a garbage collection, age, value is added by 1, GC generational age of objects stored in the header object) will be moved to the old era, did not reach the threshold of the object will be copied to the To Survivor region. Then empty Eden From Survivor region and area, the new generation of live objects are To Survivor region. Then, From Survivor district and To Survivor area will exchange their roles, that is, the new district is the last GC To Survivor emptied From Survivor district, the new district is the last GC From Survivor To Survivor's district, in short, in any case To Survivor will ensure the area after a GC is empty. When To Survivor target area did not survive the new generation of space to store a collection down the GC, you need to rely on old's allocation guarantees, these objects stored in the old era.

       Years old (Old)

       After a number in the new generation (see the specific thresholds virtual machine configuration) GC still surviving objects into the old era. The old era of object life cycle is longer, the survival rate is relatively high, relatively low frequency of GC in the old era, but the recovery pace is relatively slow.

       Permanent Generation (Permanent)

       On behalf of permanent storage class information, constants, static variables, the time compiler to compile the code and other data for the region, Java Virtual Machine Specification pointed out that garbage collection can not, in general, will not be garbage collected.

       Minor GC and the difference of Full GC

       New Generation GC (Minor GC): Minor GC refers to the place in the new generation of GC, because most of the new generation of Java objects are raw evening toward the dead, so Minor GC very frequently, usually recover relatively fast speed. When Eden is not enough space to allocate memory for the object, triggers Minor GC.

       Old's GC (Full GC / Major GC): Full GC occurs in the GC means the old era, the emergence of Full GC usually accompanied by at least one of the Minor GC (old target's mostly Minor GC into the process from the new generation years old), such as: distribution guarantees failure. Full GC generally slower than the speed of more than 10 times Minor GC. Years old when insufficient memory or an explicit call to System.gc () method, will trigger Full GC.

 Garbage collection algorithm


  Mark - sweep algorithm (Mark-Sweep)
  This is the most basic collection algorithm, as its name suggests, the algorithm is divided into "mark" and "clear" in two stages:

  First of all marked objects need to be recovered, after the completion of uniform recycling mark out all the objects are marked.

  The reason why it is the most basic collection algorithm, because subsequent collection algorithms are based on this idea and make improvements to its shortcomings obtained.

It has two main disadvantages:

  1, efficiency, marking and clearance process efficiency is not high;

  2, space issues, will have to clear after marking a plurality of discrete memory chips, too much space debris may cause, when the program needs to allocate a large object during subsequent runs can not find enough contiguous memory had to trigger advance another garbage collection action.

Replication algorithm (Copying)
In order to address efficiency issues, collection algorithm "Copy" (Copying) is called there, according to the available memory capacity will be divided into two equal size, each use only one of them. When this piece of memory runs out, the copy will also survive object to another one above, then memory space has been used once and then clean out.

Such that each time a block of memory which is recovered, will not consider the complexities of memory fragmentation isochronous memory allocation, as long as the top of the stack pointer movement, in order to allocate memory, simple, efficient operation. But the cost of this algorithm is to reduce memory by half, it would be a bit too high.

But the efficiency of this algorithm is quite high, so now the commercial virtual machines are using this collection algorithm to recover the new generation. Why the new generation of algorithms can be used to copy it?

IBM has specialized studies have shown that 98% of the new generation of objects are born dead towards evening, so you do not need a 1: 1 ratio to divide the memory space. Here In view of this, the new generation adopts the following division policy.

Now the new generation is subdivided into three parts, a larger Eden (Eden) and two smaller Survivor (survivors) region.

When recovered, and the Eden Survivor further object alive once copied to another piece of Survivor spaces, and final clean out the Eden space is just used Survivor. HotSpot VM default size ratio is 8:1 Eden and Survivor, i.e. each new generation the available memory space for the new generation of 90% of the whole capacity (80% + 10%), only 10% of the memory will be " wasteful.

After this clean-up is complete, the original Survivor on the air, and remain empty until next Minor GC, it survived in full bloom again as objects. Survivor two turns so as GC during transit station of the new generation of live objects.

However, if you use the memory area replication algorithm of a large number of live objects, replication algorithm will become stretched, then need a larger area for holding Survivor those live objects, may need even 1: 1 ratio. So for heap memory area of ​​the old era, there is the following algorithm.

Mark - Collation Algorithm
marking process is still the "mark - sweep" algorithm the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving objects are moved to the end, then clean out the memory directly outside the terminal boundary. This method avoids the generation of debris, they also do not need an extra memory space, it would be more appropriate for the old era.

But compared to replication algorithm, although less memory space occupied by the algorithm, but the cost of garbage collection time will be longer than the replication algorithm, it said above

We should try to avoid or reduce the incidence of Full GC.

   Both algorithms use refined language description is

Replication algorithm: a space for time

Mark - sorting algorithms: time for space use

Word fish and can not have both, but for the new generation and the old era, they are the best choice.

 

Summed up
simply sort out what some of the knowledge points mentioned in the text

1, in order to better manage heap memory, the area is divided into the old and the new generation's.
2, the new generation garbage collection occurs frequently than the old era.
3, garbage recycling Cenozoic become Minor GC; GC old's happened to become Full GC.
4, using a new generation of replication algorithm for garbage collection; use old's mark - Collation Algorithm
5, in order to more efficiently manage the new generation of memory, according to the replication algorithm, combined with IBM's research and feasibility studies, the new generation is divided into three, a relatively large Eden area and two smaller areas Survivor, a ratio of 8: 1: 1
as possible to avoid or reduce garbage collection

 

This article is reproduced to: https://blog.csdn.net/sumj7011/article/details/78087421

Guess you like

Origin www.cnblogs.com/zgwjava/p/10963649.html