Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of execution engine and garbage Collection in Virtual Machine

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of the execution engine and garbage collection in the virtual machine, I believe that most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it!

I. execution engine

The application program is compiled and converted into bytecode files. The bytecode loaded into the memory space can not be directly executed on the operating system. As the core part of the Java virtual machine, the execution engine interprets / compiles the bytecode instructions into local machine instructions on the corresponding system platform.

Interpreter: when the virtual machine starts, the bytecode is interpreted line by line according to the predefined definition, and the contents of each bytecode file are interpreted as local machine instructions of the corresponding system platform.

JIT compiler: the virtual machine compiles the source code into the machine language related to the local machine platform, and looks for hot and frequently executed code to put it into metaspace, that is, the JIT cache code stored in metaspace.

Garbage collection: objects that do not have any references are marked as garbage and are reclaimed to free memory space.

Second, garbage object tag 1, reference counting method

Each object keeps an integer reference counter, which is used to record the number of times the object is referenced. When the object is referenced by an object, the counter increases by 1, and when a reference is lost, the counter is minus 1. The reference counting algorithm determines whether the object can be recycled as a garbage object by judging the number of references to the object.

Although the reference counting method is efficient, it is a fatal flaw that when two objects refer to each other, the two objects will never be recycled. So JVM does not use this marking algorithm.

2. Reachability analysis algorithm

The reachability analysis algorithm is based on whether the reference chain from the object to the root object is reachable to determine whether the object can be reclaimed.

The running program regards all the reference chains as a graph, and uses the GC-Roots root object collection as the starting point to continuously search down from each root node whether the objects connected by the root object set are reachable. The search path is called the reference chain (Reference-Chain). If there is no reference chain from the object to the GC-Roots, the object is not available.

Objects referenced in the virtual machine stack

Objects referenced by class static properties in metaspace

Objects referenced by constants in metaspace

Objects referenced by Native methods in the local method stack

Compared with the reference counting algorithm, the reachability analysis algorithm avoids the problems caused by circular references, and also has the characteristics of efficient execution, which is also the marking algorithm adopted by JVM.

Garbage collection mechanism 1. Tag removal algorithm

The marking-clearing algorithm is divided into two phases: marking and clearing:

Marking phase: scan from the root object collection to mark the surviving objects; clear phase: scan again to find untagged objects and recycle them

The algorithm is not efficient, garbage collection needs to pause the application, and a large number of memory fragments will be generated. When the subsequent program allocates objects that take up a large amount of memory, there will be insufficient continuous memory, which can easily trigger another garbage collection action.

2. Tag collation algorithm

The tagging process of the tagging algorithm is similar to the tagging algorithm: the first stage: marking out junk objects; the second stage: moving all living objects to one end of the memory area; and the third stage: directly cleaning up the memory outside the boundary end, similar to the process of disk destructing.

The garbage collection algorithm is not efficient, and the object movement process needs to pause the application, which is suitable for scenarios with high object survival rate (old times).

3. Replication algorithm

The replication algorithm divides the memory into two equal blocks according to capacity, and only uses one of them at a time. When the used memory is used up, the surviving objects are copied to another free memory, and then the used memory space is cleaned at once.

The algorithm has the advantages of simple implementation and high running efficiency, but the memory space is seriously wasted, so it is suitable for scenarios with low object survival rate, such as the new generation.

4. Generation collection algorithm

At present, almost all virtual machines in the market use this collection algorithm, and the generation collection algorithm adopts different algorithm mechanisms according to the characteristics of the younger generation and the old, and the life cycle of objects in different memory regions is also different. therefore, using different collection strategies for different areas of heap memory can improve the efficiency of garbage collection. In general, the survival rate of the new generation of objects is low and the recovery is frequent, so the replication algorithm is used. If the old objects have a long life cycle and a high survival rate, they use the mark removal algorithm or tag finishing algorithm.

Generally speaking, Java heap memory can be divided into three modules: new generation, old generation and permanent generation, as shown in the following figure:

The new generation

Usually, the newly created object instances are first placed in the Cenozoic space, so the pursuit of fast collection of garbage objects, in general, the Cenozoic memory is divided into one eden area and two survivor (survivor0,survivor1) zones according to the proportion of 8:1:1, and most of the object instances are generated in the Eden area.

During garbage collection, first copy the surviving objects in eden area to S0 area, then empty eden area. When S0 area is also full, copy the surviving objects in eden area and S0 area to S1 area, then empty eden and S0 area, then exchange the roles of S0 area and S1 area. When S1 area cannot store the surviving objects in eden area and S0 area, the surviving objects will be directly stored in the old age area. When the old generation area is also full, trigger a FullGC. That is, recycling is carried out in both the new generation and the old age.

Old age

The old generation area stores some objects with a long life cycle, and the object instances that have experienced several garbage collection in the new generation will be moved to the old generation area.

The above is all the contents of the article "sample Analysis of execution engine and garbage Collection in Virtual Machine". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report