In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "analyzing Java memory management and garbage collection". In daily operation, I believe many people have doubts in analyzing Java memory management and garbage collection. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "analyzing Java memory management and garbage collection". Next, please follow the editor to study!
Java runs in a virtual memory environment created by JVM. Memory is divided into two parts: stack and heap.
Stack
The basic concept of stack is referred to on paper: stack (stack). Many languages use stack data structures to record the order of function calls and related variables (refer to Linux from program to process).
In Java, the stack in JVM records the method calls of the thread. Each thread has a stack. During the running of a thread, if there is a new method call, the corresponding stack of the thread will be added a storage unit, namely frame. In frame, the parameters, local variables, and return address of the method call are stored.
Call stack
The parameters and local variables of Java can only be basic types of variables (such as int), or references to objects (reference). Therefore, in the stack, only basic types of variables and object references are saved.
The object that the reference points to is saved in the heap. (the reference may be null, that is, it does not point to any object)
References and objects
When the called method is finished, the corresponding frame of the method will be deleted, and the space occupied by parameters and local variables will be released. The thread returns to the original method and continues execution. When all the stacks are cleared, the program runs to an end.
Heap
As mentioned above, the stack can take care of itself. But the heap must be treated with care. A heap is an area of the JVM that can be freely allocated to objects. When we talk about garbage collection (garbage collection), we mainly recycle space in the heap.
The normal object of Java lives in the heap. Unlike the stack, the space in the heap is not emptied as the method call ends. Therefore, objects created in a method can continue to exist in the heap after the method call ends. One problem with this is that if we keep creating new objects, we will eventually run out of memory.
Garbage collection
Garbage collection (garbage collection, or GC for short) can automatically empty objects in the heap that are no longer in use. Garbage collection mechanism first appeared in 1959 and was used to solve problems in the Lisp language. Garbage collection is a major feature of Java. Not all languages have garbage collection. For example, there is no garbage collection mechanism in Chammer Cobb +. Programmers need to manually free memory in the heap.
Because there is no need to manually free memory, programmers can also reduce the chance of making mistakes in programming. With garbage collection, programmers can avoid some bug associated with pointer and memory leaks (this type of bug is usually hidden). On the other hand, garbage collection takes more time to calculate. Garbage collection is actually transferring the responsibility that originally belongs to the programmer to the computer. Programs that use garbage collection take longer to run.
In Java, the object is used by reference (the object is likened to a deadly poison, and the reference is like tweezers used to extract the poison). If there are no more references to the object, we will no longer be able to call or process the object. Such an object will be unreachable. Garbage collection is used to free memory occupied by unreachable objects. This is the basic principle of garbage collection.
(the unreachable object is the dead object, the garbage to be recycled by garbage collection)
Early garbage collection adopted the mechanism of reference counting (reference counting). Each object contains a counter. When there is a new reference to the object, the counter increments 1. When the reference is removed, the counter is minus 1. When the counter is 0, it is considered that the object can be garbage collected.
One possible problem, however, is that if there are two object circular references (cyclic reference), such as two objects referencing each other, and there are no other references (pointing to An or B) at this time, we cannot actually reach the two objects by reference at all.
Therefore, we take the stack and static data as the root, start from the root, follow all references, and we can find all reachable objects. That is, an reachable object must be referenced by the root or by other reachable objects.
Orange, accessible; green, unreachable
JVM implementation
Garbage collection in JVM is a mixture of multiple mechanisms. JVM will decide which kind of garbage collection to use according to the running condition of the program.
Let's first learn about "mark and sweep". Under this mechanism, each object will have tag information to indicate whether the object is reachable or not. When garbage collection starts, the Java program pauses. JVM starts from the root, finds all the reachable objects, and marks them (mark). JVM then needs to scan the entire heap to find the remaining objects and empty the memory occupied by those objects.
The other is "copy and sweep". Under this mechanism, the heap is divided into two regions. The object always lives in one of two regions. When garbage collection starts, the Java program pauses. JVM starts from the root, finds the reachable object, copies the reachable object into a blank area and arranges it tightly, and modifies the change of the reference address caused by the object movement. Finally, directly clear the entire area where the object used to live, making it a new blank area.
As you can see, "copy and sweep" requires more complex operations, but it also allows objects to be arranged closely to avoid possible gaps in "mark and sweep". When creating new objects, "copy and sweep" can provide large chunks of contiguous space. Therefore, if the objects are relatively "longevity", then it applies to "mark and sweep". If the "metabolism" of the object is active, it is suitable for "copy and sweep".
The above two mechanisms are mixed through generational recycling (generational collection). Each object records its generation information. The so-called generation refers to the number of garbage collections experienced by the object. The older the object, the longer it lives in memory.
According to statistical observations of the Java program, the older the generation, the less likely it is to be recycled (the rich get richer and the poor get poorer). Therefore, when we are in garbage collection, we should pay more attention to those young objects.
Now, take a look at the heap in JVM:
We can see that the heap is divided into three generations. Among them, the permanent generation (permanent generation) survives the Class object. These objects are not garbage collected. We have learned in RTTI that each Class object represents a class, contains class-related data and methods, and provides the code for the class definition. Each object is created with reference to the corresponding Class object. Each object contains a reference to its corresponding Class object.
The younger generation (young generation) and the mature generation (tenured generation) need to be recycled. The object generation in the young generation is closer, while the object generation in the mature generation is longer.
Generation after generation
The younger generation is further divided into three regions
Eden (Eden): newborn objects live in this area. A new object is an object that has been created since the last GC.
The freshman lives in the Garden of Eden.
From, to: these two regions are equal in size, equivalent to two regions in copy and sweep.
When the newly created object cannot be placed in the eden area, the minor collection will be started. JVM adopts the strategy of copy and sweep to copy the reachable objects of eden area and from area to to area. After a garbage collection, the eden area and the from area are emptied, and the to area is tightly stored with living objects. Subsequently, the from area becomes the new to area, and the to area becomes the new from area.
If you find that the to area does not fit during minor collection, you will put some objects into the mature generation. On the other hand, even if the to area is not full, JVM will still move objects that are old enough to mature generations.
If the mature generation is full of objects and cannot be moved into new objects, major collection will be triggered. JVM adopts the strategy of mark and sweep to collect garbage for mature generations.
At this point, the study on "analyzing Java memory management and garbage collection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.