In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is Java virtual machine memory management knowledge". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what is Java virtual machine memory management knowledge" together!
0. Java's division of memory:
Java Virtual Machine Specification (Main memory and CPU cache, register) divided into five areas: program counter, Java virtual machine stack, local method stack, Java heap, method area, but did not specify the specific implementation of these areas, heard in other places some terms (such as permanent generation, meta-space, etc., these are the specific implementation of the method area) may be the specific implementation of these areas, this point should be paid special attention to, do not be confused by these concepts.
The characteristics of each region are as follows:
Area Thread Relationship Memory Exception Garbage Collection Operator Counter Thread Private No Record Java Virtual Machine is pointing to bytecode instruction Java Virtual Machine Stack Thread Private StackOverflowError, OutOfMemoryError No Description Java method execution memory model, stack frame stores local variable table, operand stack, dynamic link, method return address and other information. Local method stack thread Private StackOverflowError, OutOfMemoryError No description Local method (not written in Java code) Memory model at execution Method area thread sharing OutOfMemoryError stores class information loaded by virtual machine, constants (constant pool), static variables, code generated by just-in-time compiler (JIT) Java heap thread sharing OutOfMemoryError stores Java objects (instance) 1. Class loader:
Class loaders are divided into Bootstrap, Extension ClassLoader (Platform ClassLoader in Java9), Application ClassLoader, and the level is also from low to high.
You can call the getParent() method of the class loader object to find the loader one level above the class loader, which also becomes the parent class loader.
Class Loader Description Whether it is Java implementation BootstrapCreated at JVM startup, usually implemented by native code related to the operating system, is the most basic class loader, responsible for loading the most core Java classes, such as Object class, System class, String class, etc. No Extension ClassLoader loads some extended system classes, such as XML, encryption, compression related functions Class is Application ClassLoader Load user-defined CLASSPATH class is
This is not translated, after translation, it will change the flavor, especially the following Parents Delegation Model is not appropriate to translate into a parent delegation model.
Bytecode files loaded into memory, can instantiate out of the class, and class loader is responsible for loading Java classes. Lower-level class loaders will query the class loader at the next higher level before loading a class until they query the class loader at the top level.(Bootstrap), if the top-level class loader can load the class, otherwise try to load the class downward, that is, if the class loader can load the class, use the class loader at the upper level (reuse the class loader at the upper level), and then use its own class loader to load, which is word of mouth is a parent delegation model that is not properly translated. Doing so makes class loading safer and prevents breaking the virtual machine by loading classes with the same names as standard Java-like packages.
You can inherit Application ClassLoader to implement custom class loader, isolate loader, modify class loading mode, expand loading source, and prevent source code leakage.
2. The process of class loading:
Class loading is the process of instantiating a bytecode file into a Class object and initializing it. Class loading includes three steps: class loading (Load), class linking (Link) and class initialization (init).
Class loading is to read the bytecode file into memory in a binary stream and convert it into a specific data structure, check the magic number cafe baby (whether it is a Java file flag), whether there is a parent class, etc., and create a Class object corresponding to the class.
The linking of classes is divided into three stages: verification, preparation and resolution. The verification stage is to check in more detail, such as whether the type is correct and whether the static variables are reasonable. The preparation stage is to allocate memory space for the static variables of the class and set default values. The resolution stage is to ensure the correctness of mutual references between classes and classes and complete the structural layout of the class in memory.
The initialization of a class is not to initialize the object, but to initialize the static variable value of the class according to the value in the code. There are also two ways to initialize the static variable of the class: directly specifying the value at the time of declaration and specifying the value in the static code block.
There are two ways to access objects:
Local variable tables in the Java virtual machine stack store data in addition to basic data types, as well as object reference types, which are related to how to access an object.
In different virtual machines, object access methods are also different, the mainstream access methods are the use of handles and direct pointers.
Use handles:
Using handles is to divide an area in the Java heap as a handle pool, where the instance data and type data (class-related information) of the object are stored, and the address of the object in the handle is stored in the reference, which is an indirect way to access the object.
Direct pointer:
A direct pointer is the address of the object directly stored in the reference, but the Java heap needs to consider how to store pointers to access object types.
The two methods actually have their own advantages and disadvantages, as shown in the table below:
The handle reference stores a stable handle address, and the object only changes the address of the object in the handle pool when moving, while the address in the reference does not need to be changed. Indirect access to the direct pointer saves the time overhead of a pointer location, and the access speed is relatively faster. Direct access 4, determine whether the object can be recycled algorithm:
Before garbage collection, it is necessary to judge whether the object can be collected. Common judgment algorithms include reference counting algorithm and reachability analysis algorithm.
Reference counting algorithm:
Each object has a corresponding reference counter. When there is a reference to the object, the reference counter is incremented by 1. When the reference expires, the reference counter is decremented by 1. When the counter value is 0, it means that the object has no reference and can be reclaimed.
Cons: Looks simple and efficient, but has circular reference problems. If two objects contain references to each other, circular reference problems occur, causing the garbage collector to fail to reclaim the object.
Reachability analysis algorithm:
If there is no direct or indirect application relationship between the object and GC Roots, it can be recycled. Common GC Roots objects include objects referenced in the virtual machine stack (stack frame local variable table), objects referenced by static attributes in the method area, objects referenced by method area constants, and objects referenced in the local method stack (Native method). GC Roots is a special object and must not be referenced by other objects, otherwise there will be circular references like reference counting algorithms.
Note: Java engineers who have worked for 1 to 6 years are welcome to join the Java Architecture Exchange Dress: 834962734. Free Java architecture learning materials are provided within the group (Spring, MyBatis, Netty source code analysis, principles of high concurrency, high performance, distributed, microservice architecture, JVM performance optimization, etc.) These become essential knowledge bodies for architects and a roadmap for advanced Java learning.
5. Common garbage collection algorithms:
mark-and-clear algorithm
The most basic garbage collection algorithm, the subsequent algorithm is to improve it.
First mark out the objects to be recycled, and then clear the marked area.
Disadvantages are: search efficiency when marking, memory fragmentation when clearing.
marker-copy algorithm
Divide the memory area into two blocks, use only one at a time, mark the memory area in use during garbage collection, copy the surviving objects to another memory area, and then clear the original memory area at one time. Avoid memory fragmentation, but not for long-lived objects.
Disadvantages: waste half of the memory space, when the object survival rate is high, a large number of copy operations, efficiency is not high.
mark-sort algorithm
Marking process and marking-except that the algorithm is the same, garbage collection, is to move the surviving objects to the same end, and then clear the memory area outside this, so that the memory area occupied by the object is continuous, avoiding the generation of memory fragmentation.
generational collection algorithm
According to the length of the object survival time, the heap memory is divided into the new generation and the old generation. The objects with short survival time are placed in the new generation area, and the large objects with long survival time (such as object array) are placed in the old generation area. The ratio of Cenozoic to Old Cenozoic is 1 : 2. The Cenozoic is divided into one Eden zone and two Survivor zones. The new generation uses marker-copy algorithm, and the old generation uses marker-clean algorithm or marker-clean algorithm, so as to maximize the advantages of each algorithm.
6. Common garbage collectors:
Serial Reclaimer
Serial adopts "replication algorithm" implementation, if it is in a single CPU environment, Serial collector has no thread interaction overhead, theoretically can obtain the highest single-thread execution efficiency, STW time can also be controlled in tens to hundreds of milliseconds, this time is completely acceptable.
Serial Old (PS Sweep) Reclaimer
Serial Old collector is an older version of Serial collector and is also a single-threaded collector that uses a mark-and-clean algorithm.
ParNew Reclaimer
ParNew collector is actually a multithreaded version of Serial collector. The collection algorithm, STW, object allocation rules, recycling policy, etc. are exactly the same as Serial collector, and the two have a lot of the same code. Although ParNew collector has the advantage of multi-threading, but in single CPU and multi-CPU environment, the effect is not necessarily better than Serial, at least in a single CPU environment is definitely not as good as Serial.
Parallel Scavenge Reclaimer
Parallel Scavenge collector is similar to ParNew collector, it is also a new generation collector, it also uses replication algorithm, and it is also a parallel multithreaded collector. Parallel Scavenge collectors can control CPU throughput and STW time more precisely than ParNew collectors, and can complete less interactive tasks faster.
Parallel Old Reclaimer
Parallel Old collector is an older version of Parallel Scavenge collector that uses multithreading and a mark-and-tidy algorithm. Before Parallel Old collector appears, if Parallel Scavenge collector is selected as the new generation collector, Serial Old collector can only be selected as the old generation collector, which is definitely a waste of multiple CPUs. Therefore, Parallel Scavenge collector + Parallel Old collector is a strong combination for environments with high throughput requirements in multi-CPU environments.
CMS Reclaimer
CMS (Concurrent Mark Sweep) collector from the English name is based on the "mark-clean algorithm" implementation, and there are concurrent characteristics, it is a kind of STW to shorten the time as the goal of the collector, for some attention to service response speed of the website, certainly the shorter the STW, the better the user experience, but the disadvantage is that will be at the end of garbage collection will produce a lot of space debris.
Garbage collection is completed through four steps: Initial Mark, Concurrent Mark, Remark and Concurrent Sweep.
G1 Reclaimer
G1 collector is the most advanced collector at present, and it is also the default garbage collector after JDK7. It is based on the "mark-copy algorithm", so it will not produce memory fragmentation, and it can also accurately control the time of STW. The G1 collector is suitable for both the new generation and the old generation, giving priority to the areas with the most garbage.
Thank you for reading, the above is "Java virtual machine memory management knowledge what" content, after the study of this article, I believe that we have a deeper understanding of Java virtual machine memory management knowledge, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.