VM 堆和非堆

 

Heap and Non-Heap Memory
 
The Java VM manages two kinds of memory: heap and non-heap memory, both of which are created when the Java VM starts.
Java VM 管理两种类型的内存:堆和非堆,两者都在JVM启动时创建。
 
  • 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. 

  • 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.

        堆内存是一片运行时数据区域,JVM使用该块内存为所有的类实例和数组分配内存。堆大小可以是固定的或者可变的。GC是一个自动内存管理系统,它将从(不可达)对象上收回堆内存。
 
        非堆内存包括一个被所有线程共享的方法区和一块用于内部处理需要的或用于JVM优化的内存。它存储了每个类的结构,如运行时常量池,字段和方法数据,方法和构造器代码。方法区是堆的逻辑部分,但是,取决于具体的实现,JVM可以不对其进行垃圾回收或者压缩整理。类似堆内存一样,方法区大小可以是固定的或者可变的。方法区内存不需要是连续的。
 
In addition to the method area, a Java VM may require memory for internal processing or optimization which also belongs to non-heap memory. For example, the Just-In-Time (JIT) compiler requires memory for storing the native machine code translated from the Java VM code for high performance.
       除了方法区外,JVM需要内存作为内部处理或者优化,该内存属于非堆内存。比如,JIT编译器为了更好的性能,需要内存存储从JVM代码编译的成本地机器码。
 
Memory Pools and Memory Managers

Memory pools and memory managers are key aspects of the Java VM's memory system.

  • A memory pool represents a memory area that the Java VM manages. The Java VM has at least one memory pool and it may create or remove memory pools during execution. A memory pool can belong either to heap or to non-heap memory.

  • A memory manager manages one or more memory pools. The garbage collector is a type of memory manager responsible for reclaiming memory used by unreachable objects. A Java VM may have one or more memory managers. It may add or remove memory managers during execution. A memory pool can be managed by more than one memory manager.

内存池内存管理器是JVM内存系统的关键组成部分。
     
     一个 内存池代表一块JVM管理的内存区域。 JVM至少有一个内存池,它在运行期间可以创建或移除内存池。一个内存池可以属于堆,也可以属于非堆。
     
     一个 内存管理器管理一个或多个内存池。GC是一种内存管理器,它负责收回被不可达对象的使用的内存。JVM可能有一个或多个内存管理器。它可以在执行期间可以增加或者移除内存管理器。一个内存池可以
     被一个以上的内存管理进行管理。

Garbage Collection

Garbage collection (GC) is how the Java VM frees memory occupied by objects that are no longer referenced. It is common to think of objects that have active references as being "alive" and non-referenced (or unreachable) objects as "dead." Garbage collection is the process of releasing memory used by the dead objects. The algorithms and parameters used by GC can have dramatic effects on performance.

The Java HotSpot VM garbage collector uses generational GC. Generational GC takes advantage of the observation that most programs conform to the following generalizations.

  • They create many objects that have short lives, for example, iterators and local variables.

  • They create some objects that have very long lives, for example, high level persistent objects.

垃圾收集(GC)是Java虚拟机如何释放不再被引用的对象占用的内存。人们通常认为对象有两种:仍然被引用的对象是“活着”的,非引用(或无法访问)对象是“死“的。垃圾收集(GC)是一个释放被已死对象占用的内存。GC的算法和参数对性能有非常大的影响。
 
Java HotSpot VM 垃圾收集器使用分代垃圾收集。分代垃圾收集器利用了大多数程序遵循以下分代的观察结果:
 
大多数对象只有短暂的生命周期,比如,循环器和变量。
 
少数对象拥有较长的生命周期,比如,高级的持久化对象。
 
Generational GC divides memory into several generations, and assigns one or more memory pools to each. When a generation uses up its allotted memory, the VM performs a partial GC (also called a minor collection) on that memory pool to reclaim memory used by dead objects. This partial GC is usually much faster than a full GC.
 

The Java HotSpot VM defines two generations: the young generation (sometimes called the "nursery") and the old generation. The young generation consists of an "Eden space" and two "survivor spaces." The VM initially assigns all objects to the Eden space, and most objects die there. When it performs a minor GC, the VM moves any remaining objects from the Eden space to one of the survivor spaces. The VM moves objects that live long enough in the survivor spaces to the "tenured" space in the old generation. When the tenured generation fills up, there is a full GC that is often much slower because it involves all live objects. The permanent generation holds all the reflective data of the virtual machine itself, such as class and method objects.

The default arrangement of generations looks something like Figure 3-7.

分代垃圾收集器将内存分为多个代,并为每个代分配一个或多个内存池。当一个代用完了分配给它的内存,VM将在内存池中执行部分垃圾收集(也称为minor 收集),收回被已死对象占用的内存。这个局部垃圾收集通常比full GC要快很多。
 
Java HotSpot VM 定义了两个代:年轻代(有时也称作"护士")和老年代。年轻代有一个”Eden space“和两个“survivor space" 组成。VM将所有初始化对象分配至Eden space,大多数对象在此死去。当执行一次minor GC时,VM将所有活着的对象从Eden space 移到其中的一个Survivor space中。VM将那些在survivor space存活的较长的对象转移到老年代的”tenured“ space.当 tenured 代填满时,一次full GC通常较慢,因为它涉及到所有存活的对象。永久代持有所有的虚拟机本身的反射数据,比如类和方法对象。
 
代的默认安排看起来像如图3-7

Figure 3-7 Generations of Data in Garbage Collection
The different generations of data defined by the Garbage Collector

If the garbage collector has become a bottleneck, you can improve performance by customizing the generation sizes. Using JConsole, you can investigate the sensitivity of your performance metric by experimenting with the garbage collector parameters. For more information, see Tuning Garbage Collection with the 5.0 HotSpot VM.

如果垃圾收集器成为瓶颈,可以调整各个代的大小来改善性能。使用JConsole,你可以尝试通过垃圾收集器参数调查你的性能指标的灵敏度。更多信息,请参见 Tuning Garbage Collection with the 5.0 HotSpot VM.

 

你可能感兴趣的:(java,GC,VM,堆和非堆)