@867976167
2014-10-26T10:57:08.000000Z
字数 4102
阅读 2539
翻译
In Java, allocation and de-allocation of memory space for objects are done by the garbage collection process in an automated way by the JVM. Unlike C language the developers need not write code for garbage collection in Java. This is one among the many features that made Java popular and helps programmers write better Java applications.
在Java中,内存的分配与回收全部由JVM的垃圾回收进程自动完成的。与C语言不同,Java开发者不需要自己收到的实现垃圾回收。这是Java许多受欢迎的特征之一,并且帮助程序员更好的编写Java程序。
This is a four part tutorial series to know about the basics of garbage collection in Java,
下面是四篇教程让你了解Java GC(垃圾回收)的基础:
Java Garbage Collection Introduction
How Java Garbage Collection Works?
Types of Java Garbage Collectors
Monitoring and Analyzing Java Garbage Collection
This tutorial is the first part in the series. It will explain the basic terminologies like JDK, JVM, JRE, HotSpot VM then understand the JVM architecture and Java heap memory structure. It is important to understand these basic things before going into the Garbage collection tutorial.
这篇教程是系列第一部分。它将会解释基本的术语像JDK,JVM,JRE,HotSpotVM。接下来介绍JVM结构和Java 堆内存结构。理解这些基础对于理解下面的垃圾回收知识很重要。
Key Java Terminologies
Java API – is a collection of packaged libraries that helps developers to create Java applications.
Java Development Kit (JDK) – is a set of tools that enables a developer to create Java applications. JDK includes tools to compile, run, package, distribute and monitor Java applications.
Java Virtual Machine (JVM) – JVM is an abstract computing machine. Java programs are written against the JVM specification. JVM is specific for OS platform and they translate the Java instructions to the underlying platform specific instructions and execute them. JVM enables the Java programs to be platform independent.
Java Runtime Environment (JRE) – JRE comprises the JVM implementation and the Java API.
Java HotSpot Virtual Machine
Each JVM implementation may be different in the way the garbage collection principles are implemented. Prior to SUN takeover Oracle had JRockit JVM and after takeover it got the HotSpot JVM. Presently Oracle maintains both the JVM implementations and it has declared that over a period these two JVM implementations will be converged to one.
每种JVM实现可能使用不同的方法实现垃圾回收机制。在SUN被Oracle收购之前使用的是JRockit JVM,但是收购之后使用HotSpot JVM。目前Oracle拥有两种JVM的实现并且一段时间后两个JVM实现合并为一种。
HotSpot JVM is available as a core component as part of the standard Oracle SE platform. In this garbage collection tutorial, we will see the garbage collection principles based on the HotSpot Virtual Machine.
HotSpot JVM是目前Oracle SE平台标准的一部分的核心组件。在这篇垃圾回收教程中。我们将会看到基于HotSpot虚拟机的垃圾回收原则。
JVM Architecture
Following diagram summarizes the key components in a JVM. In the JVM architecture, two main components that are related to garbage collection are heap memory and garbage collector. Heap memory is the runtime data area where the instances will be store and the garbage collector will operate on. Now we know how these things fit in the larger scheme.
下面图片总结了JVM的关键组件。在JVM体系结构中,与垃圾回收相关的两个主要组件是堆内存和垃圾回收器。堆内存是内存数据区,用来保存运行时的对象实例,并且垃圾回收器也会在这里操作。现在我们知道这些组件如何在大的框架工作。
Java Heap Memory
It is essential to understand the role of heap memory in JVM memory model. At runtime the Java instances are stored in the heap memory area. When an object is not referenced anymore it becomes eligible for eviction from heap memory. During garbage collection process, those objects are evicted from heap memory and the space is reclaimed. Heap memory has three major areas,
我们有必要了解堆内存在JVM内存模型的角色。在运行时,Java的实例被存放在堆内存区域。当一个对象不再被引用时,就满足从堆内存移除的条件。在垃圾回收进程中,这些对象将会从堆内存移除并且内存空间被回收。堆内存有下面三个主要的区域:
Young Generation
Eden Space (any instance enters the runtime memory area through eden)
S0 Survivor Space (older instances moved from eden to S0)
S1 Survivor Space (older instances moved from S0 to S1)
Old Generation (instances promoted from S1 to tenured)
Permanent Generation (contains meta information like class, method detail)
- Young Generation(新生代)
S1 Survivor Space (存在时间更长的实例将会从s0移动到s1)
Old Generation(老年代)实例将从s1提升到tenured
Permanent Generation(永久代)包含像类,方法等的细节的元信息
Update: Permanent Generation (Permgen) space is removed from Java SE 8 features.
Permanent Generation 空间在Java SE 8 特性中已经被移除。
在本系列的第二篇将会介绍Java垃圾回收的工作原理
In this next part of this tutorial series we will see about how garbage collection works in Java.