Basic Principles of hotspot JVM (1)

Source: Internet
Author: User

I recently learned about JVM, mainly about hosspot. Here I will give a rough look at it.


1. Structure of the hotspot JVM

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/46/6F/wKioL1PyO4uym9mxAAGc8JnqaLY552.jpg "Title =" 1.png" alt = "wkiol1pyo4uym9mxaagc8jnqaly552.jpg"/>

Describes the general structure of hotspot. We can see that the general process of JVM is to load a class file into the system through the class loader, and then put it into different areas for compilation through the compiler.


2. Heap Structure

We all know that Java heap is divided into three generations, which are called generational management.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/46/6E/wKiom1PyO0-CNaQCAADpvh-fbFo916.jpg "Title =" 2.png" alt = "wKiom1PyO0-CNaQCAADpvh-fbFo916.jpg"/>

We can see that the three generations are the young generation, the old generation and the permanent generation. Young generation. Here is an assumption that most objects are created in the young generation when they are first created. When the age of the object reaches the upper limit of the young generation, the default value is 15. If he is still in the new generation after 15 garbage collection times, he will be promoted to the old generation.

So what is permanent generation?

Permanent generation means that this part of content is not controlled by garbage collection by default. For example, the data called by the Local interface is stored in permanent generation, when the class is loaded by the class loader, the data in the class is permanently generated. The uninstallation of these classes and the memory release of the Local interface are very strict, in this case, it is generally placed on permanent generation, and the garbage collector is not managed. Of course, there are exceptions.

The figure also shows that the new generation is divided into three zones. Eden, S0, S1, and s indicate that the interval Val is alive. These will be improved later.


3. Memory recycling and tlab

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/46/6F/wKioL1PyPYfw2I-aAAESzmE2VIk027.jpg "Title =" 3.png" alt = "wKioL1PyPYfw2I-aAAESzmE2VIk027.jpg"/>

First, let's look at the figure above. Suppose the entire figure is a memory zone, such as the new generation of Eden, how is Eden partitioned? Check only tlab1 first, which indicates a snapshot of the state of a thread during object application. That is to say, when a thread applies for the first time, it will draw a block in the memory (dark color indicates the application ), it will have a pointer pointing to the starting point (free_list) of the next continuous space of this object. If you want to apply for memory next time, use the pointer to move the size of the next application backwards, this mechanism is called bump the pointer.


Second, because an object needs to be allocated, a region belongs to this object, and other objects cannot come in. If the whole region is regarded as an object, when applying for the memory, you must synchronize the memory block. The synchronization process is implemented through the lock. That is to say, you must lock the memory when applying for the memory block, after the application is complete, the next thread will apply for the lock again. If you do not manage the memory, the efficiency of the allocation will be very low.


To improve the memory allocation efficiency, Oracle proposes a mechanism called tlab, which is short for Thread Local allocation buffers. Through the name, we can see that it roughly divides the Eden area into a region corresponding to the number of threads, and each region is allocated to a thread, that is to say, thread 1 can only allocate memory in tlab1, thread 2 can only allocate memory in tlab2, and so on.

How can I use another space?

When a thread is allocated in the current memory area, it must apply for a piece of memory in the subsequent continuous area. That is to say, this thread is associated with two tlabs, and it has a lot of buffers. To manage these buffers, The free_list of this thread points to both the occupied memory and the starting point of the new applied memory. There may be n in the following order. In this way, we can get rid of the lock mechanism, because the threads are all single-thread operations, and there is no concurrency problem. That is, we can apply for a lock only in the initial allocated area at the time of application, when the application is full, the lock is applied in consecutive regions. This is called concurrent synchronization.



4. Garbage collection Algorithm

There are three types of garbage collection algorithms in general. The first is the replication algorithm, the second is the mark clearing algorithm, and the third is the mark sorting algorithm, also known as the mark clearing compression algorithm.


(1) replication algorithm

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/46/6E/wKiom1PyPlGitWgpAADcqyBgVj8778.jpg "Title =" 4.png" alt = "wkiom1pyplgitwgpaadcqybgvj8778.jpg"/>

The replication algorithm copies the surviving objects from one space to another. The figure shows that there is a space in the upper and lower areas, and they are two memory areas of the same size. Now, assume that the Java Virtual Machine is input at a certain time, and the root object (roots) is referenced to a of the first space, and a is introduced to C, now, garbage collection is performed. Because the two pieces of memory are identical in size, you only need to copy the objects referenced by the root object in the memory above, after copying, adjust the pointer references between them.

But this has a disadvantage: in this way, the above memory area and the following memory area have only 50% utilization. To solve this problem, Oracle divides the space into three blocks, namely, the Eden, S0, S1, during his replication, he does not copy an equivalent piece of memory to another piece of memory. He copies the objects that survive Eden and S0 to S1. if S1, if you are older, you will be promoted to the old age. This method greatly improves the memory usage. This is Oracle's optimization of the replication algorithm, and this optimization is indeed effective, because most objects assume that the survival time is not very long, that is, after a memory is recycled, its usage is very low, which greatly increases the memory usage.


(2) Mark clearing Algorithm

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/46/6E/wKiom1PyP1SiygRMAAEdqyE3PfY505.jpg "Title =" 5.png" alt = "wkiom1pyp1siygrmaaedqye3pfy505.jpg"/>

Compared with the replication algorithm, the mark clearing algorithm has a similar advantage: one of its biggest advantages to the replication algorithm is to improve the memory utilization, but there is a drawback: memory fragmentation.

The initial status is still referenced by the root. During garbage collection, mark the objects that are referenced, that is, the objects that cannot be released in the garbage collection. After the object is marked, it is cleared, clearing is to traverse the memory, but this traversal is not a byte or a byte, and an algorithm will quickly become the object marked and not marked. In the figure, object B is cleared. But there is a problem with clearing. The space released by B may be used later, so there will be a pointer pointing to this place, and we will find that the areas and areas are not continuous after clearing, we call the region after B is released as memory fragments.


(3) mark and clear the Compression Algorithm

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/46/70/wKioL1PyQWHwR5ICAAEjgAG2gcM829.jpg "Title =" 6.png" alt = "wkiol1pyqwhwr5icaaejgag2gcm829.jpg"/>

The first two steps for marking and clearing the compression algorithm are the same as those for marking and clearing the algorithm. However, there will be a sorting step, which means that B occupies this place and C following B will be moved forward during recycling. After recycling, the position of C will change. The action of clearing and compressing is called sorting.

That is to say, the tag compression algorithm is an optimization of the tag clearing Algorithm on top of the tag clearing algorithm. It does not contain memory fragments, but it has a fatal drawback: During the sorting process, an object copy cost is generated. Here, C is copied to B. If ABCD and AC are referenced and BD needs to be recycled, B and D will be copied, and this copy is not a one-time copy, the copy cost will be very high, therefore, consider this algorithm with caution.


This article is from the "xlows" blog, please be sure to keep this source http://xlows.blog.51cto.com/5380484/1541823

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.