Big Data Fundamentals----knowledge points of the JVM-5 zones and garbage collection mechanism

Source: Internet
Author: User

has been to the JVM to see and forget, forget to see again. Do a note-taking today and store it here.

Let's take a look at the JVM's memory model diagram:

There are 5 districts on it, what are these 5 districts for?

Let's imagine a scene:

We have a class file in which there are many definitions of classes, where are the definitions of classes placed? The definition of a class is placed inside the method area.

The program produces many objects at run time, such as linklist, which are stored in the heap.

Stack:

Our program is essentially a startup thread at run time, such as the main function is a main thread. A main main thread can have a lot of sub-threads.

The thread is doing our various methods of working. There must be some local variables defined in the method, such as we have a new object in the method, the object must be placed in the heap, but the reference to the heap we put in the stack. Then there is a problem with the stack, we have a program has a lot of threads, if all the variables inside the thread is stored together, there will certainly be variables are duplicated, conflict. All cannot be put together. so the stack is stored in a sub-thread . each thread is its own stack space, and the thread is private.     The heap is thread-shared.

There is a detail inside the stack:

This means that a stack space is separated by different threads. Each thread has its own stack, and each thread executes a number of methods, each of which corresponds to a stack frame:

Each method executes creates a stack frame that stores information such as local variable tables, operand stacks, dynamic links, method exits, and so on. From what can be seen, each thread executes a method, which means that there is a stack frame in the stack frame corresponding to the current thread in the stack and out of the stack.

You can see that there are local variable tables in each stack frame. A local variable table holds information such as the various basic data types, object references, and so on during compilation.

OK, let's take a look at something specific:

Local method Stack ( Native Stack )

The local method stack (Native stack) is very similar to the Java Virtual Machine station (Java stack), where the difference is that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine stack, while the local method stack is used with the Native method service.

Heaps (heap)

For most applications, the Java heap is the largest piece of memory managed by a Java virtual machine, and this area is created as the virtual machine starts. In practical use, the objects and arrays we create are stored in the heap. If you hear about thread safety, it's clear that the Java heap is a shared area and that members of the operations shared area have locks and synchronizations. In the process of running the new object constantly, there is a heap inside.

Java heap is also associated with the Java garbage collection mechanism (GC), which is the primary area for garbage collector management. The concept of the new generation, Laosheng generation and permanent generation that the program ape is familiar with is in the heap, and now most of the GC basically uses the Generational collection algorithm. If you are more detailed, the Java heap also has Eden space, from Survivor space, to survivor space, and so on.

The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

The above image is the image of our new generation, the old age. involves a garbage collection mechanism.

Let's introduce the garbage collection mechanism:

The objects created at the beginning are stored in Eden Sapce (the Garden of Eden, the new Generation), and everyone is carefree. And then, the garbage collection mechanism comes. First to the object of life in the Garden of Eden Detection once, found that there is also the use of value to put objects in the survivor space inside the from Space,survivior space is divided into two pieces, one is from space, a piece is tosapce space. Tospace space is seldom used (it is used for the moving of garbage collection). The garbage collection mechanism then recycles objects from space, such as 18 of times, and survives the old Generation. We are surprised that the tospace here is used.

Okay, let's introduce the garbage collection algorithm:

There are two kinds of algorithms.

1: Object A has no reference to object B. There is no reference to garbage, there is a problem with this method. A, B can not distinguish whether it is rubbish or not by quoting each other.

2: root node search. Search down from the root node. Can search for is not garbage, can not search, is rubbish.

This is recorded by the mapping table, and then the details are not known.

The object is actually a square:

As above, red is a good object, and black is a garbage object.

How do we recycle the black objects:

Method One: Flag-clear:

Remove the black object directly.

But there is a problem: the removal of the deterioration, my space has become very fragmented. The next time you want to put a large object (must be a continuous space) such as four lattice, it will not fit.

Method Two: Mark collation:

I only move useful: Move the red useful object to a white blank place. The problem, however, is that it has an impact on the running program.

Method Three:

Replication algorithm:

To preserve a useful space in advance: that is the tospace space mentioned above.

In the Fromspace space, the garbage collection mechanism marks the useless object as black, and then transfers the red useful objects to the Tospace space on the right, until the garbage collection mechanism clears the black and red on the left. then the tospace of the right side of the red useful objects and then transplanted to the left of the fromspace space. That's neat.

Then we'll think of a problem. Isn't it a waste of space for him to reserve so much room on the right? Actually the space on the right does not need to be that big, just a little bit is enough. Such as

Because our useful objects (red squares) are actually very few. Many objects will not be used for a good time.

Method Area

The method area, like the heap (Java heap), is an area of memory shared by each thread, used to store data such as class information, constants, static variables, and instant compiler-compiled code, which are loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, she has an alias called Non-Heap (non-heap). Analysis of the Java Virtual Machine specification, the reason is to describe the method area as a logical part of the heap, you should feel that they are stored in the data point of view. A storage object data (heap), a stored static information (method area).

In the above, we see a description of the new generation, Laosheng generation, and permanent generation in the heap. Why do we say the new generation, the Laosheng generation, the permanent generation of three concepts, that is because the hotspot virtual machine design team chose to extend the GC collection to the method area, or use a permanent generation to implement the method area. This allows the hotspot garbage collector to manage this part of the memory as if it were managing the Java heap. The simple point is that the memory model of the hotspot virtual machine is divided into generations, in which the Cenozoic and Laosheng generations are implemented in the heap, using the method zone for the permanent generation. According to the official roadmap information, there is now also the abandonment of the permanent generation and gradually adopt native memory to achieve the planning of the method area, in the JDK1.7 hotspot, has been placed in the permanent generation of the string constant pool removed.

Big Data Fundamentals----knowledge points of the JVM-5 zones and garbage collection mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.