Skip to content
Java Memory Management & Garbage Collection

Summary - Java Memory Management & Garbage Collection

JVM Memory Structure

Introduction to JVM
The Java Virtual Machine (JVM) is a crucial part of the Java Runtime Environment (JRE), responsible for running Java bytecode by converting it into machine code that your computer's processor can understand.
Memory Areas in JVM
Heap Memory: Stores all Java objects, shared among all threads. Objects here are globally accessible, and memory is dynamically allocated but can lead to shortages if overused.
Stack Memory: Holds method calls, local variables, and references to objects in the Heap. Each thread has its own stack, operating on a Last In, First Out (LIFO) principle.
Metaspace: Stores metadata about classes and methods, replacing the older PermGen space from Java 8 onwards, allowing dynamic growth to avoid memory limitations.
Extra Knowledge:
JVM Class Loader: Responsible for loading class files into memory, with different types of class loaders handling core Java libraries, extensions, and application classes.

Garbage Collection in Java

Introduction
Garbage Collection (GC) automatically identifies and removes unused objects, freeing up memory and preventing leaks.
How Garbage Collection Works
Objects become eligible for GC when they are no longer referenced, such as when local variable scope ends, references are nulled, or reassigned.
Garbage Collection Algorithms
Serial GC: Uses a single thread, suitable for small applications with "Stop-the-World" (STW) pauses.
Parallel GC: Uses multiple threads to reduce pause times, ideal for high-throughput applications.
CMS (Concurrent Mark and Sweep) GC: Minimizes STW pauses by performing most tasks concurrently, suitable for low-latency applications.
G1 GC: Balances pause times and throughput, designed for large heap sizes with predictable performance.
Extra Knowledge:
Manual Memory Management in C/C++: Unlike Java, C/C++ requires manual memory management, risking memory leaks if not properly handled.

Mark and Sweep Algorithm

Introduction
The Mark and Sweep algorithm is a foundational garbage collection technique, marking active objects and sweeping away inactive ones.
Phases of Mark and Sweep
Mark Phase: Marks all reachable objects.
Sweep Phase: Reclaims memory by sweeping unmarked objects.
Challenges
STW Pauses: Requires pausing the application during both phases.
Fragmentation: Does not address memory fragmentation, leading to scattered free memory.
Fun Fact: Modern GCs like CMS and G1 enhance Mark and Sweep to reduce pauses and improve performance.

Concurrent Mark and Sweep (CMS) Algorithm

Introduction
CMS reduces "Stop-the-World" pauses by performing most garbage collection tasks concurrently.
Phases of CMS
Includes Initial Mark, Concurrent Mark, Precleaning, Final Mark, and Concurrent Sweep phases.
Generational Hypothesis
Divides the heap into Young, Old, and Permanent Generations, with frequent minor GCs and less frequent major GCs.
Fun Fact: CMS focuses on minimizing pause times, while G1 GC balances between pause times and throughput.

Memory Leaks in Java

Introduction
Memory leaks occur when objects that are no longer needed are not removed from memory, leading to increased memory usage and potential application crashes.
Causes of Memory Leaks
Static Field Holding Object Reference: Prevents objects from being garbage collected.
Unclosed Resources: Failing to close resources like file streams or database connections.
Inner Classes Referencing Outer Classes: Can prevent the outer class from being garbage collected.
Cached Objects: Without proper eviction strategies, can lead to leaks.
Detecting and Preventing Memory Leaks
Use Verbose GC Logging and Heap Dumps to identify and analyze memory leaks.
Fun Fact: Heap dump analysis can help optimize memory usage and ensure efficient application performance.
Best Practices to Avoid Memory Leaks
Understand object lifecycle and scope, use collections wisely, close resources properly, and monitor memory usage regularly.
Understanding JVM memory management and employing best practices in garbage collection and memory leak prevention are critical for creating efficient, high-performance Java applications. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.