Java Memory Management

Book description

Improve application performance by tuning, monitoring and profiling both the garbage collector and JVM

Key Features

  • Understand the different parts of Java memory and the various garbage collectors so you can select your preferred one
  • Explore how memory management can help to effectively improve performance
  • Learn how to spot and avoid memory leaks to enhance application performance

Book Description

Understanding how Java organizes memory is important for every Java professional, but this particular topic is a common knowledge gap for many software professionals. Having in-depth knowledge of memory functioning and management is incredibly useful in writing and analyzing code, as well as debugging memory problems. In fact, it can be just the knowledge you need to level up your skills and career.

In this book, you'll start by working through the basics of Java memory. After that, you'll dive into the different segments individually. You'll explore the stack, the heap, and the Metaspace. Next, you'll be ready to delve into JVM standard garbage collectors. The book will also show you how to tune, monitor and profile JVM memory management. Later chapters will guide you on how to avoid and spot memory leaks.

By the end of this book, you'll have understood how Java manages memory and how to customize it for the benefit of your applications.

What you will learn

  • Understand the schematics of debugging and how to design the application to perform well
  • Discover how garbage collectors work
  • Distinguish between various garbage collector implementations
  • Identify the metrics required for analyzing application performance
  • Configure and monitor JVM memory management
  • Identify and solve memory leaks

Who this book is for

This book is for all levels of Java professionals, regardless of whether you're a junior or senior developer, a DevOps engineer, a tester, or the system admin of a Java application. If you currently don't have in-depth knowledge of Java memory, garbage collection, and/or JVM tuning, then this book will help you to take your Java skills to the next level.

Table of contents

  1. Java Memory Management
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a Free PDF copy of this book
  6. Chapter 1: Different Parts of the Java Memory
    1. Technical requirements
    2. Understanding computer memory and Java memory
      1. Computer memory
      2. Accessing the main memory
      3. Overview of the main memory
      4. Java memory and the JVM
      5. The JVM
      6. Memory management and the JVM
      7. Memory management before Java
      8. Understanding the JVM’s components for memory management
      9. Runtime data area
    3. Creating variables in Java
      1. Primitives and reference types
    4. Storing variables on the stack
      1. Current frame and current method
      2. Elements of the frame
      3. Values on the stack
      4. Primitives and wrapper classes
    5. Creating objects in Java
    6. Storing objects on the heap
    7. Exploring the Metaspace
    8. Summary
  7. Chapter 2: Primitives and Objects in Java Memory
    1. Technical requirements
    2. Understanding primitives on the stack and heap
    3. Storing objects on the heap
      1. References
      2. Objects
      3. Understanding the differences between references and objects
    4. Managing object references and security
      1. Inspecting the escaping references issue
    5. Summary
  8. Chapter 3: Zooming in on the Heap Space
    1. Exploring the different generations on the heap
      1. Garbage collection roots
    2. Learning how the spaces are used
      1. Understanding the minor garbage collection algorithm
      2. Demonstrating the minor garbage collection algorithm in action
    3. Summary
  9. Chapter 4: Freeing Memory with Garbage Collection
    1. Technical requirements
    2. Being eligible for GC
    3. Marking by the garbage collector
      1. Stop-the-world
      2. Reference counting and islands of isolation
    4. Sweeping by the garbage collector
      1. Normal sweeping
      2. Sweeping with compacting
      3. Sweeping with copying
    5. Exploring GC implementations
      1. Generational GC
      2. Serial GC
      3. Parallel GC
      4. CMS GC
      5. G1 GC
      6. Z GC
    6. Monitoring GC
    7. Summary
  10. Chapter 5: Zooming in on the Metaspace
    1. JVM usage of the Metaspace
    2. Class loading
    3. Releasing Metaspace memory
      1. Metaspace runs out of memory
      2. Metaspace size exceeds a JVM-set threshold
      3. Garbage collection of the Metaspace
    4. Summary
  11. Chapter 6: Configuring and Monitoring the Memory Management of the JVM
    1. Technical requirements
    2. The basics of JVM tuning for memory management
    3. Obtaining relevant metrics for memory management
      1. Well-functioning memory
      2. Normal latency
      3. Level of throughput
    4. Profiling Java applications
      1. Profiling with jstat and jmap
      2. Profiling with VisualVM
    5. Tuning the configurations of the JVM
      1. Tuning the heap size and thread stack size
      2. Logging low memory
      3. Tuning the Metaspace
      4. Garbage collection tuning
  12. Chapter 7: Avoiding Memory Leaks
    1. Technical requirements
    2. Understanding memory leaks
    3. Spotting memory leaks
      1. Heap memory footprint
      2. Garbage collector activity
      3. Heap dump
    4. Avoiding memory leaks
      1. Common pitfalls and how to avoid them
    5. Summary
  13. Index
    1. Why subscribe?
  14. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a Free PDF copy of this book

Product information

  • Title: Java Memory Management
  • Author(s): Maaike van Putten, Seán Kennedy
  • Release date: November 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801812856