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

Introduction of JVM memory Model in java

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

Share

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

This article mainly explains "the introduction of JVM memory model in java". 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 "the introduction of JVM memory model in java".

The importance of JVM

First of all, you should know that to run a Java application, we must first install JDK or JRE. This is because Java applications are compiled into bytecode and then run in JVM through bytecode, and JVM is the core component of JRE.

Advantages

JVM not only undertakes the analysis (JIT compiler) and execution (Runtime) of Java bytecode, but also has an automatic memory allocation management mechanism. This mechanism can greatly reduce the risk of memory leakage and memory overflow caused by manual allocation and recycling mechanism, so that Java developers do not need to pay attention to memory allocation and recycling of each object, and thus focus more on the business itself.

Shortcoming

This mechanism not only improves the efficiency of Java development, but also makes Java developers rely too much on automation and weakens the ability to manage memory, so that the system is prone to problems such as JVM heap memory anomalies, inappropriate garbage collection (GC) and too frequent GC times, which will directly affect the performance of application services.

Memory model

The JVM memory model is divided into five areas: heap (Heap), method area (Method Area), program counter (Program Counter Register), virtual machine stack (VM Stack) and local method stack (Native Method Stack).

Among them, the heap (Heap) and method area (Method Area) are thread sharing, and the program counter (Program Counter Register), virtual machine stack (VM Stack) and local method stack (Native Method Stack) are thread isolation.

Heap (Heap)

Heap is the largest piece of memory in JVM memory, which is shared by all threads, and almost all objects and arrays are allocated to heap memory.

The reactor is divided into the Cenozoic and the old age, and the Cenozoic is further divided into the Eden region and the Survivor region. Finally, the Survivor is composed of From Survivor and To Survivor.

With the update of the Java version, there have been some new changes: > in the Java6 version, the permanent generation is in the non-heap memory area; in the Java7 version, the permanent generation static variables and runtime constant pools are merged into the heap; and in Java8, the permanent generation is replaced by metaspace (in local memory).

Why replace the permanent generation with metaspace?

In order to integrate HotSpot JVM and JRockit VM, there is no need to configure permanent generation because JRockit does not have a permanent generation.

Permanent generation memory is often out of use or memory overflows (which should be the largest piece of memory in the JVM), resulting in an exception java.lang.OutOfMemoryError: PermGen. In the JDK1.7 version, the specified PermGen zone size is 8m, because the metadata information of the classes in PermGen may be collected every time of FullGC, the recovery rate is low, and the result is not satisfactory; in addition, it is difficult to determine how much space to allocate for PermGen, and the size of PermSize depends on many factors, such as the total number of class loaded by JVM, the size of constant pools and the size of methods.

See here, naturally think of the GC recycling algorithm, do not worry, I will explain in a later article, now is still based on the JVM memory model.

Method area (Method Area)

What is the method area? > the method area is mainly used to store class-related information that has been loaded by the virtual machine, including class information, constant pool (string constant pool and all basic types have their own constant pool), and runtime constant pool. Among them, the class information includes the version, fields, methods, interfaces and parent classes of the class.

Class information

When JVM executes a class, it must go through loading, connecting and initializing, and the connection includes three stages: verification, preparation and parsing.

When loading a class, JVM loads the class file first, and in the class file there is description information such as the version, fields, methods and interfaces of the class, which is the class information.

Constant pool

In the class file, in addition to the class information, another piece of information is the constant pool (Constant Pool Table), which is used to hold various literals and symbolic references generated during compilation.

What about literals and symbolic references?

Literals include strings (String a = "b"), primitive types of constants (final-decorated variables), and symbolic references include the fully qualified names of classes and methods (such as String, whose fully qualified name is Java/lang/String), the name and descriptor of the field, and the name and descriptor of the method.

Running constant pool

When the class is loaded into memory, JVM stores the contents of the constant pool of class files in the runtime pool; during the parsing phase, JVM replaces the symbolic reference with a direct reference (the index value of the object).

For example, when a string constant in the > class is in the class file, it is stored in the class file constant pool. > > after JVM loads the class, JVM puts the string constant in the runtime constant pool and specifies the index value of the string object in the parsing phase.

The runtime pool is globally shared, and multiple classes share a runtime pool, so there is only one copy of the constant pool with multiple identical strings in the class file.

At this point, are you a little dizzy? to be honest, when I saw these contents, they were also in the clouds. Here is an example to help you understand:

Public static void main (String [] args) {String str = "Hello"; System.out.println ((str = = ("Hel" + "lo"); String loStr = "lo"; System.out.println ((str = = ("Hel" + loStr); System.out.println (str = = ("Hel" + loStr). Intern ();}

The running results are as follows:

Truefalsetrue

The first is true, because JVM automatically optimizes it into a string constant that is referenced from the same String object if it is recognized as the same string when compiled into a class file.

The second is false, because the string created at run time has a separate memory address and is not referenced from the same String object.

The last one is true, because the intern () method of String looks up whether there is an equal string in the constant pool (the result of calling the equals () method is equal), returns a reference to that string, and if not, adds its own string to the constant pool.

Error involved

OutOfMemoryError occurs when the method area cannot meet the memory allocation requirements, such as adding data to the constant pool all the time, the runtime pool will overflow and report an error.

Program counter (Program Counter Register)

The program counter is a very small memory space, which is mainly used to record the address of the bytecode executed by each thread, for example, branch, loop, jump, exception, thread recovery and so on.

Because Java is a multithreaded language, when the number of threads executing exceeds the number of CPU, threads will compete for CPU resources according to time slice polling. If a thread runs out of time slices, or some other reason causes the thread's CPU resources to be snatched ahead of time, the exiting thread needs a separate program counter to record the next running instruction.

Thus it can be seen that the program counter is related to context switching.

Virtual machine stack (VM Stack)

The virtual machine stack is the private memory space of the thread, which is created with the Java thread. > > when creating a thread, a thread stack is applied in the virtual machine stack to save the local variables, Operand stack, dynamically linked method and return address of the method, and to participate in the call and return of the method. > > the call of each method is accompanied by the stack operation of the stack frame, and the return of the method is the unstack operation of the stack frame.

It can be understood that the virtual machine stack has a corresponding thread stack for all threads in the current Java application, each thread stack is independent and independent of each other, and the unique information of the thread is stored in it.

Error involved

StackOverflowError occurs when stack memory is set to a fixed value, and this error is thrown when the stack memory required for program execution exceeds the set fixed value.

OutOfMemoryError occurs when stack memory is set to grow dynamically, and this error is thrown when JVM tries to request more memory than its available memory.

Local method stack (Native Method Stack)

> the function of the local method stack is similar to that of the virtual machine stack. The virtual machine stack is used to manage Java method calls, while the local method stack is used to manage local method calls. > > but native methods are not implemented in Java, but in the C language.

In other words, there is no code logic we wrote in the local method stack, which is decorated by native and implemented by the C language.

Thank you for reading, the above is the content of "introduction of JVM memory model in java". After the study of this article, I believe you have a deeper understanding of the introduction of JVM memory model in java, 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.

Share To

Internet Technology

Wechat

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

12
Report