Fundamentals 6 min read

Introduction to Java Memory Allocation and Garbage Collection

This tutorial introduces Java's automatic memory allocation and garbage collection managed by the JVM, explains key terminology such as JDK, JVM, JRE, outlines the JVM architecture and heap memory structure—including young, old, and permanent generations—providing essential fundamentals for understanding Java GC.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Introduction to Java Memory Allocation and Garbage Collection

Java's memory allocation and reclamation are fully handled automatically by the JVM's garbage collection process. Unlike C, Java developers do not need to write code to perform garbage collection. This is one of the many features that makes Java popular, helping programmers write Java programs more easily.

The following four tutorials form the foundation for understanding Java garbage collection (GC):

Introduction to Garbage Collection

How Garbage Collection Works

Types of Garbage Collection

Monitoring and Analyzing Garbage Collection

This tutorial is the first part of the series. It first explains basic terms such as JDK, JVM, JRE, and HotSpot VM. Then it introduces the JVM architecture and Java heap memory structure. Understanding these basics is important for grasping later garbage collection concepts.

Java Key Terminology

Java API: a set of packaged libraries that help developers create Java applications.

Java Development Kit (JDK): a suite of tools that help developers create Java applications. The JDK includes tools for compiling, running, packaging, distributing, and monitoring Java applications.

Java Virtual Machine (JVM): the JVM is an abstract computing architecture. Java programs are written according to JVM specifications. The JVM is platform‑specific and can translate Java bytecode into underlying system instructions for execution. The JVM ensures Java's platform independence.

Java Runtime Environment (JRE): the JRE includes the JVM implementation and the Java API.

Java HotSpot Virtual Machine

Different JVM implementations may use different approaches to implement garbage collection. Before acquiring Sun, Oracle used the JRockit JVM; after the acquisition, it uses the HotSpot JVM. Currently Oracle has two JVM implementations, and after some time they will be merged into one.

The HotSpot JVM is currently a core component of the standard Oracle SE platform. In this garbage collection tutorial, we will explore the garbage collection principles based on the HotSpot virtual machine.

JVM Architecture

The image below summarizes the key components of the JVM. In the JVM architecture, the two main components related to garbage collection are the heap memory and the garbage collector. Heap memory is the memory area that stores runtime object instances. The garbage collector operates here as well. Now we understand how these components work within the framework.

Java Heap Memory

It is necessary to understand the role of heap memory in the JVM memory model. At runtime, Java instances are stored in the heap memory area. When an object is no longer referenced, it becomes eligible for removal from the heap. During garbage collection, these objects are removed from the heap and the memory space is reclaimed. The heap memory consists of three main regions:

Young Generation

Eden space (where all instances initially enter the runtime memory area).

S0 Survivor space (where longer‑lived instances move from Eden).

S1 Survivor space (where even longer‑lived instances move from S0 Survivor).

Old Generation (instances promoted from S1 to the Tenured generation).

Permanent Generation (contains metadata such as classes and methods).

The Permanent Generation space was removed in Java SE 8.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaJVMGarbage Collection
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.