[Java,2017-05-15] Memory recycling (process, time, object, correlation algorithm)

Source: Internet
Author: User

Process of memory recycling

Java garbage collection is divided into three regions in the new generation, the old age, the permanent generation

When an object is instantiated go to the Garden of Eden there is not enough space:
If there is no garbage collection, the object is stored directly in Eden;
If the Eden memory is full, a minor GC will be performed;
Then judge whether the memory in the Garden of Eden is sufficient;
If not enough, see if the memory in the surviving area is sufficient;
If enough memory is available, Save the Eden part of the active object in the survival area and save the object in the Garden of Eden;
If the memory is insufficient, send the request to the old age, query the old memory is sufficient;
If there is enough memory in the old age, the active objects of some surviving areas will be stored in the old age. Then put the active object of the Garden of Eden into the survival area, the object is still in the Garden of Eden;
If the old age memory is not enough, will be a full GC, then the old generation will then make a judgment on whether the memory is sufficient, if enough ibid;
If insufficient, OutOfMemoryError will be thrown.

time of memory reclamation

GC (Memory reclamation) in Java is divided into 2 types, minor GC and full GC (also known as Major GC).

Minor GC Trigger conditions: In most cases, allocations are made directly in the Eden area. If there is not enough space in the Eden area, a Minor GC is initiated;

Trigger conditions for full GC (Major GC): Also if there is not enough space in the old age, then a full GC will be performed.

Ps: Above is just a general case, in fact, the issue of a space allocation guarantee needs to be considered:

Before the minor GC occurs, the virtual opportunity first checks whether the largest available contiguous space in the old age is greater than the total space of all objects in the Cenozoic. If the minor GC is greater than, if it is less than the Handlepromotionfailure setting allows the guarantee to fail (the direct full GC is not allowed). If allowed, then continue to check whether the largest available contiguous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than the attempt to minor GC (if the attempt fails also triggers the full GC), and if less than the full GC.

However, it is impossible to predict exactly what time it will be executed, which is determined by the system.

Memory-Reclaimed objects

Mainly according to the accessibility analysis algorithm, if an object is unreachable, then it can be recycled; If an object is available, the object cannot be recycled.

Just as the car needs a license plate number, when defining a variable, you need to declare it, declare it, get a license plate number, get a car when new, delete the variable, take the license plate number, and not recycle the car immediately. For a vehicle without a license plate, it is an unreachable object that will be reclaimed when garbage is collected (rather than deleting the variable name to reclaim the space occupied by the variable name).

related algorithms for memory recycling

1. Mark Clear Algorithm Mark-sweep

This algorithm is the simplest and most straightforward algorithm, but also some of the original ideas of other algorithms. The tag cleanup algorithm is actually the memory of the object in order to judge, if the object needs to be recycled then hit a tag, if the object still needs to use, then preserved. So after an iteration, all objects are filtered (anti-theft connection: This article starts from http://www.cnblogs.com/jilodream/) is broken once. The pair that is already marked in memory is immediately
The image is cleared in turn. This algorithm is relatively simple and rough, the implementation is relatively simple.
But it leaves two more troublesome questions:
(1) Marking and clearing requires two cycles of memory objects, the markup itself is a cumbersome task, so the efficiency of this algorithm is not particularly high.
(2) for allocated memory, it is often better to be continuous, because it facilitates the allocation of large data objects. If the current memory is a small segment of memory fragmentation, you will know that you need to allocate a large segment of memory, there is no place to put, and trigger memory reclamation. That is, lack of space leads to frequent GC and performance degradation.

2, Replication algorithm copying

During my use of the database, I had encountered a problem in which the amount of data in the table was relatively large, about 300,000 rows, and needed to delete most of the data (and therefore not the index) using several harsh conditions, leaving only the lesser of the data. The regular delete syntax is used to time out. So I looked at the maintenance staff's SQL and found a very interesting logic. First find the data that needs to be kept in the database table and put it in a temporary table. Then delete the original data sheet completely. Then change the table name of the temporary creation table to the original table name. This is a typical way of exchanging space for time. And the replication algorithm is such a way of thinking. In the replication algorithm, the memory is divided into two equal-sized memory regions A/b, and the resulting data will be stored in zone A, when the remaining space in zone A is not sufficient to hold the next newly created object, the system will copy all valid objects in zone A to zone B, and is continuously stored. It then empties all the objects in zone a directly. Since most of the objects in the programming language are created soon after creation (anti-theft connection: This article starts from http://www.cnblogs.com/jilodream/) will be recycled, so we need to copy not many objects. The implementation in Java is like this: In Java, the Eden and survivor areas are used as areas of the replication algorithm. Survivor is divided into from and to areas. This piece of content can refer to my other blog, the blog has a detailed introduction: Http://www.cnblogs.com/jilodream/p/6147791.html. The valid objects in the from area of Eden and survivor are marked each time the GC is copied to the survivor to area. Then completely erase the original Eden and from areas of the memory objects. At the same time the to area is the next reclaimed from area.

The disadvantage of the replication algorithm is that the algorithm uses space for the idea of time, so a blank area is required as the area where the memory object is to be pasted. This will undoubtedly create a waste. Especially when memory is low. Each time the algorithm clears an invalid object, a copy-and-paste object is transferred, so there is a limit to the usage scenario. This algorithm has the highest price/performance ratio only when the active object occupies a very small amount of total recycled memory. Otherwise, the amount of time wasted by a large number of copy actions may be much greater than the gain from space for time. Therefore, this algorithm is only used in the JVM as a primary object for recycling. Because of the lowest proportion of effective objects at this time, the algorithm cost-effective is the highest.

3. Mark-compact algorithm for labeling and sorting

The replication algorithm requires an additional amount of memory space to hold the surviving memory objects. This has undoubtedly caused a waste of memory. We can also put forward the optimization scheme on the basis of the original Mark clearing algorithm. That is, the available objects that are marked are moved to one side, and then the unexpected memory is removed directly from the available object boundaries. (Anti-Theft connection: This article is starting from http://www.cnblogs.com/jilodream/) This solves both the memory fragmentation problem. Do not need the original space to change the time of the hardware waste. Due to the many surviving objects in the old age, the object memory consumption is large. This makes it possible that once memory is recycled, there are not many objects that need to be recycled, and the fragments are relatively small. So there is no need for too much copy and move steps. So this method is often used in the old age.

The disadvantage of the tagging algorithm is that the tagging algorithm needs to move the object to the other side constantly, and this constant movement is very unsuitable for the small memory object. Every move and calculation is a very complex process. Therefore, in the usage scenario, it is doomed to limit the use of the tagging algorithm is not suitable for frequently creating and reclaiming objects in memory.

4, generational collection algorithm generational Collection

This algorithm divides the memory into generations, and then uses the most cost-effective algorithm to deal with the situation respectively. In Java, the heap is generally divided into the old and the new generation. The newly created objects are often placed in the Cenozoic. And after a constant recovery, the gradually surviving objects are placed in the old age. The more new objects are likely to be recycled, the older objects will survive longer. Therefore, for these two scenarios, the new generation and the old age will also be cleaned up using the two algorithms described earlier.

RELATED links:

1.51943379

2.78147364

3.https://www.cnblogs.com/jilodream/p/9038853.html

[Java,2017-05-15] Memory recycling (process, time, object, correlation algorithm)

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.