共计 4175 个字符,预计需要花费 11 分钟才能阅读完成。
Java Garbage Collection Introduction
原文地址 https://javapapers.com/java/j…
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 垃圾回收的基本知识:
Java Garbage Collection Introduction
- Java 垃圾回收介绍
How Java Garbage Collection Works?
- Java 垃圾回收的运行机理
Types of Java Garbage Collectors
- Java 垃圾回收器的类型
Monitoring and Analyzing Java Garbage Collection
- Java 垃圾回收的监控与分析
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、HotSpot VM,以帮助读者了解 JVM 的架构和 Java 堆内存的结构。想要深入了解垃圾回收的话,了解这些基本概念十分重要。
Key Java Terminologies
Java 关键术语
Java API – is a collection of packaged libraries that helps developers to create Java applications.
- Java API:打包好的一系列类库的集合,用于帮助开发者创建 Java 应用。
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 Development Kit(JDK):包含一系列工具,包括编译、运行、打包、分发和监控 Java 应用,开发者用它来创建 Java 应用。
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 Virtual Machine(JVM):JVM 是一个抽象的计算环境,Java 程序是针对这个环境的标准而编写的。不同操作系统中的 JVM 实现方式不同,但都会将 Java 指令翻译成平台特有的指令来执行。这样 Java 程序就可以是与平台无关的了。
Java Runtime Environment (JRE) – JRE comprises the JVM implementation and the Java API.
- Java Runtime Environment(JRE):JRE 包含了 JVM 实现和 Java API。
Java HotSpot Virtual Machine
Java HotSpot 虚拟机
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.
不同的虚拟机实现在垃圾回收的实施原则上有所不同。在 SUN 被 Oracle 收购之前,其产品是 JRockit 虚拟机,之后是 HotSpot 虚拟机。目前 Oracle 同时维护这两个虚拟机实现,并计划在将来某个时间将两者结合到一起。
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 虚拟机是标准的 Oracle SE 平台的核心部分。在本教程中,我们将了解基于 HotSpot 虚拟机的垃圾回收原则。
JVM Architecture
JVM 架构
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 架构中,两个部件与垃圾回收相关:堆内存(Heap Memory)和垃圾回收器(Garbage Collector)。堆内存是存放数据的内存区域,用来在运行期间保存对象实例,垃圾回收器的工作也是在堆内存当中进行的。现在我们已经在大尺度方面了解了它们之间是如何配合的。
Java Heap Memory
Java 堆内存
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)
(S0 幸存区,存在时间较长的对象将从 Eden 区移至此处) - S1 Survivor Space (older instances moved from S0 to S1)
(S1 幸存区,存在时间更长的对象将从 S0 区移至此处)
- Eden Space (any instance enters the runtime memory area through eden)
- Old Generation (instances promoted from S1 to tenured)
(老年区,又叫终身 (tunured) 区,长期存储的对象从 S1 区移至此处) - Permanent Generation (contains meta information like class, method detail)
(永久区,用于存储元数据比如类和方法)
Update: Permanent Generation (Permgen) space is removed from Java SE 8 features.
更新:从 Java 8 开始,堆内存模型中不再包含永久区。
In this next part of this tutorial series we will see about how garbage collection works in Java.
下一篇文章我们再了解垃圾回收的运行机理。