Analysis of overflow of Java method area and runtime constant pool

The runtime constant pool is a part of the method area, which is used to store class related information, such as class name, access modifier, constant pool, field description, method description, etc.

  String. Intern () is a native method. Its function is: if the string constant pool already contains a string equal to this string object, return the string object representing this string in the pool; Otherwise, add the string contained in this string object to the constant pool and return a reference to this string object. At jdk1 In version 6 and earlier, since the constant pool is allocated in the permanent generation (i.e. the method area), we can limit the size of the method area through - XX: permsize and - XX: maxpermsize, so as to indirectly limit the capacity of the constant pool. Note that jdk1 7. Gradually start "removing permanent generation". The code is as follows:

Note that VM args is a parameter for configuring VM, which is configured in the following figure:

Operation results:

It can be seen from the running results that the constant pool overflows at runtime. The prompt message followed by outofmemoryerror is "permgen space", It indicates that the runtime constant pool belongs to the method area (the permanent generation in the hotspot virtual machine). However, running this program with jdk1.7 will not get the same result, but the following prompt message will appear, because these two parameters are no longer used in jdk1.7.

If in jdk1 Run runtimeconstantpooloom in 7 For Java programs, the while loop will always run. However, the while loop does not always run until the heap memory in the system runs out. Generally, it takes a long time to appear, but the author has not tested it locally. Because in jdk1 The constant pool in 7 no longer stores objects, but object references. The real objects are stored in the heap. Put runtimeconstantpooloom The VM parameter of Java runtime is changed as follows:

Results after running the program:

An exception message appears: Java Lang. outofmemoryerror: GC overhead limit exceeded. There is no hint that there is a problem with the heap or the persistent generation. The virtual machine just tells you that your program spends too much time on garbage collection, but it doesn't work. By default, if you spend 98% of your time on GC and recycle less than 2% of the space, the virtual machine will throw this exception. This is a good practice of rapid failure security. It can be seen from the running results that after we limit the size of the heap, the program will run abnormally soon. The exception information is the same as previously thought, that is, the constant pool no longer stores objects, but object references, and the real objects are stored in the heap. About jdk1 7. For the implementation of string constant pool, a more meaningful impact can be extended here, as shown in the following code:

This code is in jdk1 6, you will get two false, and in jdk1 7, you will get a true and a false. The reason for the difference is: in jdk1 In 6, the intern () method will copy the string encountered for the first time to the permanent generation, and the returned string is also the reference of the string in the permanent generation. The string instance created by StringBuilder is in the Java heap, so it must not be the same reference, and false will be returned. And jdk1 7 (and some other virtual machines, such as JRockit), the intern () implementation will no longer copy the instance, but record the first instance reference in the constant pool. Therefore, the reference returned by Intern () is the same as the string created by StringBuilder. The reason why the STR2 comparison returns false is that the "Java" string has appeared before the execution of stringbuilder(), and its reference already exists in the string constant pool, which does not comply with the "first occurrence" principle, while the "computer software" string appears for the first time, so it returns true. If in hello If the following code is added to Java, the returned result is also false, proving that the "main" string has also appeared before.

reference resources

1. In depth understanding of Java virtual machine 2.4 Chapter 3

2. String constant pool in Java - technology black house

3. Where is the Java permanent generation

4. Deeply understand outofmemoryerror

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>