In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the memory management mode of Java virtual machine". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "memory management mode of Java virtual machine".
When it comes to memory management, you first need to understand its memory model.
The memory model of the virtual machine has changed after jdk1.8. Let's look at it separately. Please take a look at the following figure:
From the figure, we can see that each version of jdk has a new generation and an old age, the only difference is that the version less than 1.8 is permanent, while the version greater than or equal to 1.8 removes the permanent generation and becomes Meta Space.
Permanent generation is the method area in the stored data area. If a PermSpace overflow occurs while the program is running, it means that the permanent generation memory is not enough, and the JVM parameters need to be adjusted to increase the permanent generation memory space.
Jdk1.8 after the emergence of MetaSpace, which is different from the permanent generation, its memory space is dynamically expanded, of course, we can also set MaxMetadaSpace to set the maximum amount of meta-space memory, that is, setting PermSize after 1.8 is invalid.
This article mainly explains the memory management mechanism of jdk1.8 before.
In modern programming languages, there are two main ways for garbage collection algorithms: reference counter and reachability analysis.
Reference counter
The principle of reference counting is something like this: set a reference count for each created object, increase the reference count by 1 every time the object is referenced once, and subtract 1 when the object reference expires. When the reference count is 0, it means that there are no object references, and the object can be released.
One drawback of reference counting is that it cannot solve the problem of mutual references between objects, such as the following code:
Public class RefrenceCountingGC {private Object instance = null; public static void main (String [] args) {RefrenceCountingGC gc1 = new RefrenceCountingGC (); RefrenceCountingGC gc2 = new RefrenceCountingGC (); gc1.instance = gc2; gc2.instance = gc1; gc1 = null; gc2 = null; System.gc ();}}
Two objects are always in the phase of referencing each other, because the reference count can never be 0, so it cannot be released automatically.
Accessibility analysis
In order to solve these problems of reference counting, the reachability analysis algorithm appeared.
In mainstream programming languages, both java and c # determine whether an object is alive or not through reachability analysis.
Its principle is roughly as follows: through a series of objects called "GC Roots" as the starting point, and then searching down from these nodes, the search path forms a line, which we call "reference chain". Currently, an object does not have any reference chain to GC Roots, that is, when the object is unreachable, it shows that the object can be recycled. It can be better understood by the following figure:
When an object is unreachable, it is not possible to code that the object can be immediately recycled, it will be in a reprieve state, and an object needs to go through at least two tags when it is really to be reclaimed. The prerequisite for the first tag is to see if the object overrides the finalize () method or whether the finalize () method has been called by the virtual machine, and if the finalize () method is overridden and the virtual machine has not yet called it, it will be marked, and the object will still have a chance to survive, with many methods, such as referencing it in the memory of the finalize () method.
If the virtual machine has already called the finalize () method during the second collection, it will not be called again, and the object will actually be declared dead.
Take a look at the following code:
Public class GCRoot {private static GCRoot instance = null; @ Override protected void finalize () throws Throwable {super.finalize (); System.out.println ("finalized execution"); instance = this;} public static void main (String [] args) throws Exception {instance = new GCRoot (); instance = null; System.gc (); Thread.sleep If (null! = instance) {System.out.println ("object rescue succeeded!") ;} else {System.out.println ("object released!") ;} instance = null; System.gc (); if (null! = instance) {System.out.println ("object saved successfully!") ;} else {System.out.println ("object released!") ;} run result: the finalized execution object is saved successfully! The object is released! It's easy for java to refer to references before jdk1.2. We won't talk about it here, but we'll focus on references after jdk1.2.
In java, references are divided into strong references, soft references, weak references, and virtual references.
Strong citation
One of the most common types of references in java programs, such as Object o = new Object (), is never recycled by the garbage collector as long as the strong reference is there.
Soft reference
Soft references are usually used to describe objects that can be used but are not necessary. Objects associated with soft references will be recycled before memory overflow, and a memory overflow exception will be thrown only if there is still not enough memory after collection.
Weak reference
Weak references are used to describe non-essential objects, but they are weaker than soft references. Objects associated with weak references can only survive until the next time the garbage collector works. When the garbage collector starts to work, the objects associated with weak references will be recycled regardless of whether the current memory is sufficient or not.
Virtual reference
Virtual reference is the weakest type of reference, and the only purpose of setting a virtual reference relationship for an object is to receive a system notification when the object is garbage collected.
1. Mark-clear algorithm
This is the most basic garbage collection algorithm, all subsequent algorithms are extended on the basis of this algorithm.
As can be seen from the name of the algorithm, the algorithm is divided into two stages: "marking" and "clearing": first, all the objects that need to be recycled need to be marked, and all the marked objects need to be cleared after the marking is completed. There are two main shortcomings of this algorithm: one is the performance problem, the performance of marking and clearing is not high, and the other is that a large number of memory fragments of the product will be discontinuous after mark cleanup. Too much memory fragmentation will cause the next time when you need to allocate objects that take up a lot of memory, you can't find enough consecutive fragments and have to trigger the garbage collection action again.
2. Replication algorithm
In order to solve the problem of efficiency, the replication algorithm appeared. This algorithm divides the available memory area into two blocks of the same size, and when garbage collection is needed, the available objects are first copied to another memory area, thus clearing the current area at once. The advantage of this is that a whole area of memory is cleared at a time, thus avoiding a large amount of memory fragmentation.
At present, most of the mainstream commercial virtual machines use replication algorithms to recover the new generation.
3. Marking-finishing algorithm
When the survival rate of the object is high, the replication algorithm is used, the efficiency will be very low, so the replication algorithm is generally not used in the old years.
In view of this problem, an idea called "tag-collation" algorithm has emerged. Like the tag-clear algorithm, it needs to be tagged first, but it does not simply clear the marked objects at once, but moves all living objects to the other end, and then clears the objects outside the boundary.
4. Generation collection algorithm
Generational collection actually divides the memory into several pieces, such as dividing the java heap into the new generation and the old age, and adopting the most suitable algorithm according to the age, so this method is adopted by the mainstream commercial virtual machines at present.
Thank you for reading, the above is the content of "memory management of Java virtual machine". After the study of this article, I believe you have a deeper understanding of the memory management of Java virtual machine, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.