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

How to understand the memory management of Java virtual machine

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to understand the memory management of Java virtual machine, which may not be understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

The Java virtual machine specification divides physical memory (main memory and caches and registers in CPU) into five areas: program counter, Java virtual machine stack, local method stack, Java stack and method area, but does not specify the specific implementation of these areas. some nouns heard in other places (such as permanent generation, metaspace, etc., these are specific implementations of method areas) may be specific implementations of these areas, which should be paid special attention to. Don't be confused by these concepts.

Next, let's learn more about it with the editor.

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 classloader object to find the upper-level loader of that level loader, which is also the parent class loader.

You can inherit Application ClassLoader to implement a custom class loader as needed, isolate the loader, modify the loading mode of the class, extend the 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 link (Link) and class initialization (init).

The loading of the class is to read the bytecode file into memory as a binary stream and convert it into a specific data structure, check the magic number of cafe baby (whether it is the flag of the Java file), whether there is a parent class, etc., and create the corresponding Class object of the class.

The link of the class is divided into three stages: verification, preparation and parsing, and the verification stage is to check in more detail, such as whether the type is correct, whether the static variable is reasonable, etc.; the preparation stage is to allocate memory space for the static variables of the class, and set the default value; the parsing phase is to ensure the correctness of the mutual reference between the class and the class, and to complete the structural layout of the class in memory.

3. There are two ways to access objects:

The data stored in the local variable table in the Java virtual machine stack contains not only the basic data types, but also the object reference type (reference), which is related to how to access an object.

In different virtual machines, the access methods of objects are also different, and the mainstream access methods are handle and direct pointer.

4. An algorithm for determining whether an object can be recycled:

Before garbage collection, it is necessary to judge whether the object can be recycled or not. the common judgment algorithms are reference counting algorithm and reachability analysis algorithm.

Reference counting algorithm:

Each object has a corresponding reference counter. When there is a place to reference the object, the value of the reference counter is increased by 1. When the reference expires, the value of the reference counter is subtracted by 1. When the value of the counter is 0, it means that the object has no reference and can be recycled.

Cons: it looks simple and efficient, but there is a circular reference problem. If two objects contain references to each other, there will be a circular reference problem, resulting in the garbage collector cannot collect 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 virtual machine stack (stack frame local variable table), objects referenced by static properties in method area, objects referenced by constant in method area, and objects referenced in local method stack (Native method). GC Roots is a special object and must not be referenced by other objects, otherwise it will have the problem of circular reference like the reference counting algorithm.

5. Common garbage collection algorithms:

Mark-clear algorithm

The most basic garbage collection algorithm, the follow-up algorithm is to improve it.

First mark the objects that need to be recycled, and then clear the marked area content.

The disadvantages are: the search efficiency when marking, and memory fragmentation when clearing.

6. common garbage collectors:

Serial recycler

Serial Old (PS MarkSweep) recycler

ParNew recycler

Parallel Scavenge recycler

Parallel Old recycler

CMS recycler

G1 recycler

After reading the above, do you have any further understanding of how to understand Java virtual machine memory management? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report