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's the difference in Java8 memory structure?

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

Share

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

Editor to share with you what is the difference in the memory structure of Java8, I hope you will gain something after reading this article, let's discuss it together!

1. JVM memory distribution

According to the JVM specification, JVM memory is divided into five parts: virtual machine stack, heap, method area, program counter and local method stack.

1. Virtual machine stack: each thread has a private stack, which is created as the thread is created. What is stored in the stack is something called "stack frame". Each method creates a stack frame in which local variables (basic data types and object references), Operand stack, method exit and other information are stored. The size of the stack can be fixed or dynamically expanded. When the stack call depth is greater than the range allowed by JVM, an error of StackOverflowError will be thrown, but this depth range is not a constant value. We can test this result through the following program:

Stack overflow test source code:

After running it three times, you can see that the depth of each stack is different, and the output is as follows.

As for how the value in the red box comes out, it needs to go deep into the source code of JVM before it can be discussed, which is not elaborated here.

In addition to the above error, another error in the virtual machine stack is that it throws an OutOfMemoryError when the space is not available. One small detail to note here is that catch captures Throwable, not Exception. Because neither StackOverflowError nor OutOfMemoryError belongs to a subclass of Exception.

2. Local method stack:

This part is mainly related to the Native method used by the virtual machine, and in general, Java application programmers do not need to care about this part.

3. PC register:

PC register, also known as program counter. JVM supports multiple threads running at the same time, each with its own program counter. If the JVM method is being executed, the register holds the address of the currently executing instruction; if the native method is executed, the PC register is empty.

4. Heap

Heap memory is a part shared by all threads of JVM and is created when the virtual machine starts. All objects and arrays are allocated on the heap. This part of the space can be recycled through GC. OutOfMemoryError will be thrown when the space is not available. Let's simply simulate a heap memory overflow:

Run the above code, and the output is as follows:

Note that here I specify a heap memory size of 16m, so the count=14 displayed in this place (this number is not fixed), as to why it is 14 or other numbers, need to be judged by the GC log, the specific reasons will be explained in the next article.

5. Method area:

The method area is also shared by all threads. It is mainly used to store class information, constant pool, method data, method code and so on. The method area is logically part of the heap, but it is often called a "non-heap" in order to distinguish it from the heap. The issue of overflows in the method area will be discussed in detail below.

II. PermGen (permanent generation)

Most Java programmers should have seen the exception "java.lang.OutOfMemoryError: PermGen space". The "PermGen space" here actually refers to the method area. However, there is an essential difference between the method area and "PermGen space". The former is the specification of JVM, while the latter is an implementation of the JVM specification, and only HotSpot has "PermGen space", while for other types of virtual machines, such as JRockit (Oracle) and J9 (IBM), there is no "PermGen space". Because the method area mainly stores the relevant information of the class, it is easy to have a permanent memory overflow in the case of dynamically generated classes. The most typical scenario is that when there are too many jsp pages, permanent generation memory overflows are easy to occur. We now simulate the memory overflow of "PermGen space" by dynamically generating classes:

The running results are as follows:

The JDK version used in this example is 1.7, and the size of the specified PermGen zone is 8m. Load the Test class by generating a different URLClassLoader object each time, resulting in a different class object, so you can see the familiar "java.lang.OutOfMemoryError: PermGen space" exception. JDK 1.7 is used here because in JDK 1.8, HotSpot no longer has a "PermGen space" interval and is replaced by something called Metaspace (metaspace). Let's look at the difference between Metaspace and PermGen space.

3. Metaspace (metaspace)

In fact, the work of removing the permanent generation began with JDK1.7. In JDK1.7, some of the data stored in the permanent generation has been transferred to Java Heap or Native Heap. But the permanent generation still exists in JDK1.7 and has not been completely removed, such as the transfer of symbolic references (Symbols) to native heap; literals (interned strings) to static variables of the java heap; class (class statics) to java heap. We can compare JDK 1.6 with JDK 1.7 and JDK 1.8 through a program, taking string constants as an example:

This program constantly generates new strings with an exponential order of 2, which consumes memory more quickly. We run JDK 1.6,1.7 JDK 1.and JDK 1.8respectively:

The running result of JDK 1.6is:

The running result of JDK 1.7:

The running result of JDK 1.8:

As can be seen from the above results, the memory overflow of "PermGen Space" will occur under JDK 1.6and heap memory overflow will occur in JDK 1.7and JDK 1.8.And PermSize and MaxPermGen in JDK 1.8 are no longer valid. Therefore, it is possible to roughly verify the conclusion that JDK 1.7 and 1.8 transfer string constants from permanent generations to the heap, and that permanent generations no longer exist in JDK 1.8. Now let's take a look at what metaspace is.

The essence of meta-space is similar to that of permanent generation, which is the realization of the legal area in the JVM specification. However, the biggest difference between metaspace and permanent generation is that metaspace is not in the virtual machine, but uses local memory. Therefore, by default, the size of metaspace is limited by local memory only, but you can specify the size of metaspace with the following parameters:

-XX:MetaspaceSize, the initial space size, which triggers garbage collection to unload types, and GC adjusts the value: if a large amount of space is freed, the value is lowered appropriately; if little space is freed, the value is increased appropriately when the MaxMetaspaceSize is not exceeded.

-XX:MaxMetaspaceSize, the maximum space, there is no limit by default.

In addition to the above two options that specify the size, there are two GC-related properties:

-XX:MinMetaspaceFreeRatio, after GC, the minimum percentage of Metaspace remaining space capacity, reducing garbage collection caused by space allocation

-XX:MaxMetaspaceFreeRatio, the percentage of the maximum Metaspace remaining space capacity after GC, reducing garbage collection caused by freeing space

Now let's rerun code snippet 4 under JDK 8, but this time PermSize and MaxPermSize are no longer specified. Instead, specify the size of MetaSpaceSize and MaxMetaSpaceSize. The output is as follows:

From the output, we can see that there is no permanent generation overflow this time, but a metaspace overflow.

After reading this article, I believe you have a certain understanding of "what is the difference in Java8 memory structure". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

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