In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "garbage collection mechanism and common algorithms of garbage collection in Java language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Garbage collection of Java language
In order to make programmers focus more on the implementation of the code without thinking too much about memory release, there is an automatic garbage collection mechanism in the Java language, which is the familiar GC.
With the garbage collection mechanism, programmers only need to care about the application of memory, and the release of memory is automatically recognized by the system.
In other words, the automatic garbage collection algorithm will become very important, if the algorithm is unreasonable, resulting in memory resources have not been released, may also lead to memory overflow.
Of course, in addition to Java, languages such as C # and Python also have automatic garbage collection mechanisms.
The principle of reference counting method for garbage collection algorithms commonly used in Java
Suppose there is an object A, any object's reference to A, then the reference counter of object An is + 1. When the reference fails, the reference counter of object An is-1. If the value of the counter of object An is 0, it means that object A has no reference and can be recycled.
Pros and cons analysis
advantage
With high real-time performance, it does not need to wait until there is not enough memory to start recycling. At run time, it can be recycled directly according to whether the counter of the object is 0 or not.
During garbage collection, the application does not need to be suspended. If there is insufficient memory when applying for memory, report it immediately.
Outofmember error.
Culture, when an object's counter is updated, only that object is affected, not all objects are scanned.
Inferior position
Every time an object is referenced, the counter needs to be updated, which is a little time-consuming.
Waste of CPU resources, even if there is enough memory, still count the counters at run time.
Unable to resolve circular reference problem. (biggest drawback)
Circular problem demo:
Class TestA {public TestB b;} class TestB {public TestA a;}
Public class Main {public static void main (String [] args) {TestA a = new TestA (); TestB b = new TestB (); a.b = b; b.a = a; a = null; b = null;// is set to null, but there is still a problem of circular reference between an and b} marking-clearing algorithm principle
The label removal algorithm divides garbage collection into two stages, namely, marking and clearing.
Tag: marks the referenced object starting from the root node.
Cleanup: objects that are not referenced by tags are junk objects and can be cleaned up.
In the initial state, all target objects are 0 (unmarked)
When jvm runs out of valid memory, it suspends the thread, executes the GC thread, and marks it.
Mark from the root node to the end, and then recycle the untagged objects.
After cleaning up, suspend the gc thread and re-execute the previously suspended thread.
The marked object will be reset to 0.
Pros and cons analysis
advantage
It obviously solves the problem that the recycling application can not be recycled.
Shortcoming
The shortcomings are also obvious.
Inefficient, marking and clearing both actions need to traverse all objects, and in GC, you need to stop the application, which is a very poor experience for applications that require high interactivity.
The memory cleared by the tag removal algorithm is fragmented seriously, because the recycled objects may exist in every corner of the memory, so the cleaned memory is incoherent.
Principle of marking-compression algorithm
The tag compression algorithm is optimized and improved on the basis of the tag removal algorithm. Like the tag removal algorithm, it starts from the root node and marks the references of the objects. in the cleaning phase, it is not simply to clean up the untagged objects, but to compress the surviving objects to one end of memory, and then clean up the garbage outside the boundary, thus solving the problem of fragmentation.
Pros and cons analysis
Advantages
The problem of generating fragments is solved on the basis of the tag removal algorithm.
Shortcoming
The algorithm has one more step of compression, so it will have an impact on performance.
Principle of replication algorithm
The core of the replication algorithm is to divide the original memory space into two, use only one of them at a time, copy the objects in use to another memory space during garbage collection, and then empty the memory space and swap the roles of two memories to complete garbage collection.
The implementation of a typical replication algorithm is: the gc strategy of the younger generation of heap memory in jvm (see the part of the memory model of my jvm blog for details)
At the beginning of GC, objects only exist in the Eden area and the Survivor area named "From", and the Survivor area "To" is empty.
Immediately after that, all living objects in the GC,Eden area will be copied to "To", while those still alive in the "From" area will decide where to go according to their age. Objects that reach a certain age threshold (age threshold, which can be set by-XX:MaxTenuringThreshold) are moved to the older generation, and objects that do not reach the threshold are copied to the "To" area.
After this GC, the Eden area and From area have been emptied. At this time, "From" and "To" will hand over their roles, that is, the new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. In any case, the Survivor area named To is guaranteed to be empty.
GC repeats this process until the "To" area is filled and the "To" area is filled, and all the elephants are moved to the older generation.
Pros and cons analysis
advantage
In the case of many garbage objects, the efficiency is higher.
After cleaning, there is no debris in the memory.
Inferior position
It is not applicable when there are few junk objects, such as old memory.
The allocated 2 pieces of memory space can only be used half at the same time, and the memory utilization rate is only 50%.
Generation algorithm
The previous introduction of a variety of recycling algorithms, each algorithm has its own advantages and disadvantages, no one can replace each other, so it is a wise choice according to the characteristics of garbage collection objects.
In fact, the generation algorithm is like this, which is selected according to the characteristics of the recycled object. In jvm, the younger generation is suitable to use the replication algorithm, and the old generation is suitable to use the tag removal or tag compression algorithm.
This is the end of the introduction of "garbage collection mechanism and common algorithms of garbage collection in Java language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.