Home > Java > javaTutorial > What is a heap? What is the method area? Introduction to the heap and method area in the JVM memory model

What is a heap? What is the method area? Introduction to the heap and method area in the JVM memory model

不言
Release: 2018-09-15 17:43:04
Original
12040 people have browsed it

The content of this article is about what is a heap? What is the method area? The introduction of the heap and method area in the JVM memory model has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Heap

1.1. What is a heap

The heap is a memory area used to store objects. Therefore, it is the main target of garbage collector (GC) management. It has the following characteristics:

  • The heap is logically divided into the "new generation" and the "old generation". Since most of the objects in JAVA are born and destroyed, and a small number can reside in the memory for a long time, in order to achieve the most effective recycling of these two objects, the heap is divided into the new generation and the old generation, and the execution is different. recycling strategy. Different garbage collectors have different recycling mechanisms for these two logical areas, which we will explain in detail in subsequent chapters.

  • The memory occupied by the heap does not require physical continuity, only logical continuity.

  • The heap is generally implemented as an expandable memory size. Use "-Xms" and "-Xmx" to control the minimum and maximum memory of the heap, and the expansion action is performed by the virtual machine. However, since this behavior consumes performance, the maximum and minimum memory of the heap are generally set to be equal.

  • The heap is a memory area shared by all threads, so each thread can get the same object on the heap.

  • The life cycle of the heap is created when the virtual machine is started.

1.2. Heap exception

When the heap cannot allocate object memory and cannot be expanded, an OutOfMemoryError exception will be thrown.

Generally speaking, a GC will be performed when the heap cannot allocate objects. If the object is still unable to be allocated after GC, a memory exhaustion error will be reported. This situation can be simulated by continuously generating new objects without releasing references:

/**
 * java堆溢出demo
 * JVM参数:-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 * Created by chenjunyi on 2018/4/25.
 */
public class HeapOOM {

    static class OOMObject {
    }

    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<>();
        //不断创建新对象,使得Heap溢出
        while (true) {
            list.add(new OOMObject());
        }
    }

}
Copy after login

In the above code, objects are continuously created without reference release, causing the GC to be unable to reclaim the heap memory, and eventually OutOfMemoryError , Error message:

java.lang.OutOfMemoryError: Java heap space
Copy after login

2. Method Area

2.1. What is the method area

Method area, also called non-heap (Non- Heap), which is another memory area shared by threads. It mainly stores loaded class bytecode, class/method/field and other metadata objects, static-final constants, static variables, code compiled by the JIT compiler and other data. In addition, the method area contains a special area "runtime constant pool", their relationship is shown in the following figure:

##(1) Loaded class bytecode: To use a class, its bytecode first needs to be loaded into the memory of the JVM. As for the source of class bytecode, it can be diverse, such as .class files, network transmission, or directly generated by the cglib bytecode framework.

(2) Metadata objects such as class/method/field: After the bytecode is loaded, the JVM will generate Class/Method/Field and other objects for this class based on the content. They are used to describe a class, usually in It is often used in reflection. Unlike Java instance objects stored in the heap, these two objects are stored in the method area.
(3) static-final constants and static variables: For these two types of class members, the JVM will create a copy of data for them in the method area, so there is only one copy of static-modified class members of the same class;
(4) Compilation results of the JIT compiler: Taking the hotspot virtual machine as an example, it will use the JIT just-in-time compiler to optimize the hotspot code at runtime. The optimization method is to compile the bytecode into machine code. Normally, the JVM uses the "interpretation and execution" method to execute bytecode. That is, when the JVM reads a bytecode instruction, it will execute stack operations according to predetermined rules, and the stack operations will be further mapped to the underlying Machine operation; after JIT compilation, the executed machine code will directly deal with the underlying machine. As shown in the figure below:

2.2. Runtime constant pool

In section 2.1, we learned about the class The bytecode will be parsed when loading and different things will be generated and stored in the method area. The bytecode of a class not only contains description information such as the class version, fields, methods, interfaces, etc., but also contains a constant pool. The constant pool is used to store all literals and symbol references (such as string literals) used in bytecode. When the class is loaded, they enter the runtime constant pool in the method area for storage.

The runtime constant pool is a special part of the method area and is dynamic. That is to say, in addition to writing the constant pool into it when the class is loaded, constants can also be written into it during the running of the Java program. :

1 //使用StringBuilder在堆上创建字符串abc,再使用intern将其放入运行时常量池
2 String str = new StringBuilder("abc");
3 str.intern();
4 //直接使用字符串字面量xyz,其被放入运行时常量池
5 String str2 = "xyz";
Copy after login

2.3. Implementation of method area

The implementation of method area is not clearly stipulated in the virtual machine specification. There are currently two mainstream implementation methods:


(1)HotSpot虚拟机1.7-:在JDK1.6及之前版本,HotSpot使用“永久代(permanent generation)”的概念作为实现,即将GC分代收集扩展至方法区。这种实现比较偷懒,可以不必为方法区编写专门的内存管理,但带来的后果是容易碰到内存溢出的问题(因为永久代有-XX:MaxPermSize的上限)。在JDK1.7+之后,HotSpot逐渐改变方法区的实现方式,如1.7版本移除了方法区中的字符串常量池。

(2)HotSpot虚拟机1.8+:1.8版本中移除了方法区并使用metaspace(元数据空间)作为替代实现。metaspace占用系统内存,也就是说,只要不碰触到系统内存上限,方法区会有足够的内存空间。但这不意味着我们不对方法区进行限制,如果方法区无限膨胀,最终会导致系统崩溃。

  我们思考一个问题,为什么使用“永久代”并将GC分代收集扩展至方法区这种实现方式不好,会导致OOM?首先要明白方法区的内存回收目标是什么,方法区存储了类的元数据信息和各种常量,它的内存回收目标理应当是对这些类型的卸载和常量的回收。但由于这些数据被类的实例引用,卸载条件变得复杂且严格,回收不当会导致堆中的类实例失去元数据信息和常量信息。因此,回收方法区内存不是一件简单高效的事情,往往GC在做无用功。另外随着应用规模的变大,各种框架的引入,尤其是使用了字节码生成技术的框架,会导致方法区内存占用越来越大,最终OOM。

2.4.方法区异常

  在2.3一节中,我们了解到方法区的2种实现方式最终都会有一个最大值上限,因此若方法区(含运行时常量池)占用内存到达其最大值,且无法再申请到内存时,便会抛出OutOfMemoryError。

  在下面的例子中,我们将使用cglib字节码生成框架不断生成新的类,最终使方法区内存占用满,抛出OutOfMemoryError:

/**
 * java方法区溢出OutOfMemoryError(JVM参数适用于JDK1.6之前,借助CGLIB)
 * JVM参数:-XX:PermSize=10M -XX:MaxPermSize=10M
 * Created by chenjunyi on 2018/4/26.
 */
public class JavaMethodAreaOOM {

    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> methodProxy.invokeSuper(objects, args));
            enhancer.create();
        }
    }

    static class OOMObject {
    }

}
Copy after login

报错信息为:

1 Caused by: java.lang.OutOfMemoryError: PermGen space
2   at java.lang.ClassLoader.defineClass1(Native Method)
3   ···
Copy after login

其实,在日常开发中,不仅仅使CGlib字节码生成框架会产生大量的class信息,动态语言、JSP、基于OSGI的应用都会在方法区额外产生大量的类信息。

The above is the detailed content of What is a heap? What is the method area? Introduction to the heap and method area in the JVM memory model. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template