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 JVM metadata area

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 "what is the JVM metadata area". 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 "what is the JVM metadata area".

Metadata area

The concept of metadata area appeared after Java8 and became method area before Java8. Metadata area is also a memory area shared by threads, which is mainly used to store class information, constants, static variables and code compiled by real-time compiler.

Because the information stored in metadata is not easy to change, it is placed in an out-of-heap memory and the size is specified by-XX:MaxMetaspaceSize.

Public class MetaSpaceTest {

Public static void main (String [] args) {int I = 0; try {for (I = 0; I)

< 100000; i++) { new CglibBean(new HashMap()); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { System.out.println("total create count:" + i); } } public static class CglibBean { public CglibBean(Object object) { Enhancer enhancer = new Enhancer(); enhancer.setUseCache(false); enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) ->

Obj); enhancer.setSuperclass (object.getClass ()); enhancer.create ();}

The above code generates a large number of HashMap proxies through Cglib. Let's specify the following parameters when running this code

-XX:MaxMetaspaceSize=100M-XX:+PrintGCDetails

When our program loops to 3660 times, that is to say, we have a memory overflow in the metadata area after about 3660 proxy classes have been generated. Change the MaxMetaspaceSize to 50m to execute.

From the figure above, we can see that a memory overflow occurred in the metadata area after we generated 1710 proxy classes. It can be seen that the size of a metadata area determines the number of classes that can be loaded by the Java virtual machine.

Running constant pool

There is also an area in the metadata area called the runtime constant pool, which is used for constants generated during the run of the program, as well as various literals and symbols generated at compile time to refer to the content loaded by the class.

There are about three constant pool concepts stored in Java, so let's take a look at the other two constant pools in Java to help readers understand the differences between them.

First of all, when we understand the constant pool, we should not simply understand it as a variable modified by final, constant here means everything immutable, including final-modified variables, literals, fully qualified names of classes and interfaces, fields, method names and modifiers.

Class file constant pool

The class file constant pool refers to the Constant_Pool items in the .class file. As shown in the figure below, the class file constant pool stores literal and symbolic references.

Not all literals are stored in the class file constant pool, for example, for the integer number within the method (note that the method), if the value is between-32768 and 32767, it will be directly embedded in the JVM instruction and not stored in the constant pool. So the reader will not know the symbolic reference with CONSTANT_Integer_info 1 in the constant pool.

The class file constant pool is generated during the compilation period. When JVM loads the class file, it will replace the direct reference with the matching reference in the class file constant pool, and the loaded class file information will be stored in the runtime pool.

String pool

String pool storage JDK1.6 was previously stored in the permanent section, but was later transferred to the heap after JDK1.7.

Public static void intern () {String S1 = new String ("he") + new String ("llo"); String S2 = s1.intern (); System.out.println (S1 = = S2);}

The above code will create six objects in JDK1.6. First, new String ("he") will create an object on the heap, and the literal "he" will create an object on the string pool in the permanent area. New String ("llo") similarly creates two objects, and the last + creates another object. When the intern () method is called, it will first look for the string pool to find the object with hello content. If no object is found, another object will be created in the permanent zone, so there are a total of 6 objects. Since S1 is an object in the heap and S2 is an object in the permanent zone string pool, the s1==s2 result is false, as shown in the following figure

However, the effect is no longer like this after JDK1.6, because the string constant pool is moved to the heap, and the intern method is optimized. After JDK1.6, the above code will create five objects. First, new String ("he") will create an object on the heap, and the literal "he" will also create an object on the heap. New String ("llo") similarly creates two objects, and the last + creates another object. When intern is called, it will first look for objects with hello content in the string pool and find none. Instead of actively creating objects at this time, they will first look up objects with hello content in the heap. If so, point the pointer directly to the example in the heap, so a total of 5 objects will be created here. Since S1 and S2 point to the same object instance, s1==s2 is true, as shown in the following figure.

problem

To help readers really understand the string constant pool, here are two pieces of code. Please give the results in your mind before performing Coding verification.

Public static void intern () {String S1 = new String ("he") + new String ("llo"); String S2 = s1.intern (); String S2 = "hello"; System.out.println (S1 = = S2); System.out.println (S1 = = S2);} public static void intern () {String S3 = "hello"; String S1 = new String ("he") + new String ("llo"); String S2 = s1.intern () System.out.println (S1 = = S3); System.out.println (S1 = = S2);} Thank you for reading, the above is the content of "what is the JVM metadata area". After the study of this article, I believe you have a deeper understanding of what the JVM metadata area is, 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