java内存池是如何划分的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1262328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:32:48  来源:igfitidea点击:

How is the java memory pool divided?

javamemorypool

提问by Dani Cricco

I'm currently monitoring a Java application with jconsole. The memory tab lets you choose between:

我目前正在使用 jconsole 监视 Java 应用程序。内存选项卡可让您选择:

Heap Memory Usage
Non-Heap Memory Usage
Memory Pool “Eden Space”
Memory Pool “Survivor Space”
Memory Pool “Tenured Gen”
Memory Pool “Code Cache”
Memory Pool “Perm Gen”

What is the difference between them ?

它们之间有什么区别?

采纳答案by dfa

Heap memory

堆内存

The heap memory is the runtime data area from which the Java VM allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. The garbage collector is an automatic memory management system that reclaims heap memory for objects.

堆内存是 Java VM 为所有类实例和数组分配内存的运行时数据区域。堆的大小可以是固定的,也可以是可变的。垃圾收集器是一种自动内存管理系统,可以回收对象的堆内存。

  • Eden Space: The pool from which memory is initially allocated for most objects.

  • Survivor Space: The pool containing objects that have survived the garbage collection of the Eden space.

  • Tenured Generationor Old Gen: The pool containing objects that have existed for some time in the survivor space.

  • Eden Space:最初为大多数对象分配内存的池。

  • Survivor Space:包含在 Eden 空间的垃圾收集中幸存下来的对象的池。

  • Tenured GenerationOld Gen:包含在幸存者空间中存在一段时间的对象的池。

Non-heap memory

非堆内存

Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. The method area is logically part of the heap but, depending on the implementation, a Java VM may not garbage collect or compact it. Like the heap memory, the method area may be of a fixed or variable size. The memory for the method area does not need to be contiguous.

非堆内存包括所有线程共享的方法区和 Java VM 内部处理或优化所需的内存。它存储每个类的结构,例如运行时常量池、字段和方法数据,以及方法和构造函数的代码。方法区在逻辑上是堆的一部分,但根据实现,Java VM 可能不会对其进行垃圾收集或压缩。与堆内存一样,方法区的大小可能是固定的,也可能是可变的。方法区的内存不需要是连续的。

  • Permanent Generation: The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.

  • Code Cache: The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

  • 永久代:包含虚拟机本身的所有反射数据的池,例如类和方法对象。对于使用类数据共享的Java VM,这一代分为只读和读写区域。

  • 代码缓存:HotSpot Java VM 还包括一个代码缓存,其中包含用于编译和存储本机代码的内存。

Here's some documentation on how to use Jconsole.

这里有一些关于如何使用 Jconsole 的文档

回答by user2767149

With Java8, non heap region no more contains PermGen but Metaspace, which is a major change in Java8, supposed to get rid of out of memory errors with java as metaspace size can be increased depending on the space required by jvm for class data.

在 Java8 中,非堆区域不再包含 PermGen,而是包含 Metaspace,这是 Java8 中的一个重大变化,应该消除 java 的内存不足错误,因为元空间大小可以根据 jvm 用于类数据所需的空间来增加。

回答by Pythoner

The new keyword allocates memory on the Java heap. The heap is the main pool of memory,accessible to the whole of the application. If there is not enough memory available to allocate for that object, the JVM attempts to reclaim some memory from the heap with a garbage collection. If it still cannot obtain enough memory, an OutOfMemoryError is thrown, and the JVM exits.

new 关键字在 Java 堆上分配内存。堆是主要的内存池,整个应用程序都可以访问。如果没有足够的内存可用于为该对象分配,JVM 会尝试通过垃圾回收从堆中回收一些内存。如果仍然无法获得足够的内存,则抛出 OutOfMemoryError 并退出 JVM。

The heap is split into several different sections, called generations. As objects survive more garbage collections, they are promoted into different generations. The older generations are not garbage collected as often. Because these objects have already proven to be longer lived, they are less likely to be garbage collected.

堆被分成几个不同的部分,称为代。随着对象在更多的垃圾收集中存活下来,它们被提升到不同的代。老一代不会经常被垃圾收集。因为这些对象已经被证明寿命更长,所以它们不太可能被垃圾收集。

When objects are first constructed, they are allocated in the Eden Space. If they survive a garbage collection, they are promoted to Survivor Space, and should they live long enough there, they are allocated to the Tenured Generation. This generation is garbage collected much less frequently.

当对象第一次被构造时,它们被分配在 Eden Space 中。如果他们在垃圾收集中幸存下来,他们将被提升到 Survivor Space,如果他们在那里活得足够长,他们就会被分配到 Tenured Generation。这一代垃圾收集的频率要低得多。

There is also a fourth generation, called the Permanent Generation, or PermGen. The objects that reside here are not eligible to be garbage collected, and usually contain an immutable state necessary for the JVM to run, such as class definitions and the String constant pool. Note that the PermGen space is planned to be removed from Java 8, and will be replaced with a new space called Metaspace, which will be held in native memory. reference:http://www.programcreek.com/2013/04/jvm-run-time-data-areas/

还有第四代,称为永久代,或 PermGen。驻留在此处的对象不符合垃圾收集条件,并且通常包含 JVM 运行所需的不可变状态,例如类定义和 String 常量池。请注意,永久代空间计划从 Java 8 中删除,并将替换为一个名为 Metaspace 的新空间,该空间将保存在本机内存中。参考:http://www.programcreek.com/2013/04/jvm-run-time-data-areas/

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Ravindra babu

Java Heap Memoryis part of memory allocated to JVM by Operating System.

Java堆内存是操作系统分配给JVM的一部分内存。

Objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected.

对象驻留在称为堆的区域中。堆是在 JVM 启动时创建的,并且在应用程序运行时可能会增加或减少其大小。当堆满时,垃圾被收集。

enter image description here

在此处输入图片说明

You can find more details about Eden Space, Survivor Space, Tenured Space and Permanent Generationin below SE question:

您可以在以下 SE 问题中找到有关Eden Space、Survivor Space、Tenured Space 和 Permanent Generation 的更多详细信息:

Young , Tenured and Perm generation

年轻、终身和烫发一代

PermGen has been replaced with Metaspace since Java 8 release.

自 Java 8 发布以来,PermGen 已被 Metaspace 取代。

Regarding your queries:

关于您的疑问:

  1. Eden Space, Survivor Space, Tenured Space are part of heap memory
  2. Metaspace and Code Cache are part of non-heap memory.
  1. Eden Space、Survivor Space、Tenured Space 是堆内存的一部分
  2. 元空间和代码缓存是非堆内存的一部分。

Codecache:The Java Virtual Machine (JVM) generates native code and stores it in a memory area called the codecache. The JVM generates native code for a variety of reasons, including for the dynamically generated interpreter loop, Java Native Interface (JNI) stubs, and for Java methods that are compiled into native code by the just-in-time (JIT) compiler. The JIT is by far the biggest user of the codecache.

代码缓存:Java 虚拟机 (JVM) 生成本机代码并将其存储在称为代码缓存的内存区域中。JVM 生成本机代码的原因有很多,包括动态生成的解释器循环、Java 本机接口 (JNI) 存根,以及由即时 (JIT) 编译器编译为本机代码的 Java 方法。到目前为止,JIT 是代码缓存的最大用户。