(1/4)Java 垃圾回收介紹

Java Garbage Collection Introduction

原文地址 https://javapapers.com/java/j...java

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 垃圾回收的基本知識:segmentfault

  1. Java Garbage Collection Introduction
    Java 垃圾回收介紹
  2. How Java Garbage Collection Works?
    Java 垃圾回收的運行機理
  3. Types of Java Garbage Collectors
    Java 垃圾回收器的類型
  4. 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 同時維護這兩個虛擬機實現,並計劃在未來某個時間將二者結合到一塊兒。app

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)。堆內存是存放數據的內存區域,用來在運行期間保存對象實例,垃圾回收器的工做也是在堆內存當中進行的。如今咱們已經在大尺度方面瞭解了它們之間是如何配合的。this

JVM-Architecture

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 對象實例在程序運行期間存儲在堆內存中。當一個對象再也不被外部引用時,它就有了被回收的資格。在垃圾回收處理期間,此類對象都會被回收,其佔用的內存會被釋放。堆內存由三個主要區域組成:spa

  1. Young Generation
    (新生代)操作系統

    1. Eden Space (any instance enters the runtime memory area through eden)
      (「伊甸園」區,新的對象實例會在該區域中建立)
    2. S0 Survivor Space (older instances moved from eden to S0)
      (S0 倖存區,存在時間較長的對象將從 Eden 區移至此處)
    3. S1 Survivor Space (older instances moved from S0 to S1)
      (S1 倖存區,存在時間更長的對象將從 S0 區移至此處)
  2. Old Generation (instances promoted from S1 to tenured)
    (老年區,又叫終身(tunured)區,長期存儲的對象從 S1 區移至此處)
  3. Permanent Generation (contains meta information like class, method detail)
    (永久區,用於存儲元數據好比類和方法)

Java-Heap-Memory

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.
下一篇文章中,咱們將瞭解垃圾回收的運行機理。

相關文章
相關標籤/搜索