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

What is the class loading and memory area of JVM

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what is the JVM class loading and memory area, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. The process of loading classes on JVM

Load, load is on demand, and the main thread needs a class to start loading a class.

Verification, in short, is to verify that the class content conforms to the specified specification

Ready, allocate memory space for class variables, and initial defaults.

Initialize, execute the initialization code of the class (static code block, static assignment code). Note: initialization verifies whether the parent class is initialized. If not, the parent class must be initialized first.

Use

Unloading

II. Class loader types

Launch the class loader-Bootstrap ClassLoader, which is used to load class in the lib directory under the Java core class library-java installation directory.

The extension class loader-Extension ClassLoader, is used to load some of the extension class libraries under java-class under the java installation directory lib\ ext.

The application class loader-Application ClassLoader, loads the class under the project ClassPath.

Custom class loader, self-defined class loader

Parental appointment mechanism

It means that the parent ClassLoader is given priority to load. The reason is to avoid repeated loading and ensure the uniqueness of the Class object under the applied classPath in memory.

Hierarchical diagram

Tomcat destroys parents' appointment

Tomcat, as a java-encoded web container, also runs in jvm. How to make each web application based on tomcat container run normally, so that the class of each web application will not be messed up? it is necessary to destroy the parent delegation mechanism and load the class of each web application with a different class loader.

Class loader hierarchy diagram for tomcat

Where:

Common, catalina, and shared are responsible for loading tomcat's own core class libraries, webApp loads class of web applications (isolating the three-party class libraries referenced by web applications), and jsp loaders load JSP (each JSP file corresponds to a Jsp class loader, and jsp loaders support hot loading, that is, whenever Jsp is modified, it reloads to overwrite the original jsp).

Through tomcat's class loading mechanism, the following goals can be achieved:

Tomcat supports isolation of different versions of three-party class libraries in various web references.

The same version of the same third-party class library can be shared among different web applications.

The class library that tomcat itself depends on needs to be isolated from the class library that the application depends on.

Jsp needs to support customized development of various class loaders without restarting tomcat for the above class loading isolation and class updates without restarting.

The role of a custom class loader

In practice, you can get an bytes array from a Web server, database, or cache server, which is not what the system class loader can do.

You can get different class objects of the same class from different paths, you can achieve isolation, a complex program may be organized by module, different modules may use the same class, but use different versions, if you use the same class loader, they can not coexist, different modules can use different class loaders to achieve isolation, Tomcat uses it to isolate different Web applications OSGI uses it to isolate different modules.

Hot deployment can be achieved. Using the same ClassLoader, the class will only be loaded once, after loading, even if the class file has changed, load again, the result is still the original Class object, while using a custom class loader, you can first create a new ClassLoader, and then use it to load Class, the resulting Class object is new, thus achieving dynamic updates.

Third, JVM memory area partition method area (after 1.8is called metaspace- metadata space)

Storing the metadata information of the class can be understood as reflecting the information of the Class object.

Program counter

Record the location of bytecode instructions in the currently executed class file

It has an one-to-one relationship with threads, and each thread has its own program counter.

Stack

A program counter corresponds to a thread, a thread corresponds to a stack, a stack consists of stack frames, and a method corresponds to a stack frame. the stack frame contains a local variable table, an Operand stack, a dynamic link, and a method exit (equivalent to the position where you need to jump at the end of the method). The stack frame goes into the stack when the method is called and off the stack when the method ends.

Heap

Store object data (only references to objects (that is, addresses of objects in heap memory) or values of basic data types exist in the above stack)

Shared by all threads

When the reference variable address data is off the stack, the memory release in the heap is reclaimed by JVM control.

Other memory areas

Executing the native method will have a local method stack corresponding to the thread.

The ByteBuffer class method in NIO: allocateDirect (int capacity)-returns DirectBuffer for out-of-heap memory allocation, which can be manipulated through the Api of DirectBuffer.

Illustration

On what is JVM class loading and memory area to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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