Book Image

Java Memory Management

By : Maaike van Putten, Dr. Seán Kennedy
Book Image

Java Memory Management

By: Maaike van Putten, Dr. Seán Kennedy

Overview of this book

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.
Table of Contents (10 chapters)

What this book covers

Chapter 1, Different Parts of Java Memory, covers the different parts of Java memory: the stack, the heap, and the Metaspace. We’ll start with the stack memory and how variables are stored on the stack. We’ll then move on to deal with objects and how these are stored on the heap. Next, we’ll briefly discuss accessing primitives and objects. Finally, we’ll describe the Metaspace and what it’s used for.

Chapter 2, Primitives and Objects in Java Memory, zooms in on primitives and objects in Java memory. We’ll be dealing with the heap and stack in more detail here. With the use of visualization, we’ll show what happens during the execution of a Java program with the stack and the heap memory. Once the basics of memory management are clear, we’ll deal with object references in more detail. We explain how Java’s call-by-value mechanism, when applied to references, can lead to a security issue known as escaping references. We discuss how to solve this issue.

Chapter 3, Zooming in on the Heap Space, focuses on the different parts of the heap space. It has two main areas: the young generation space and the tenured space. The young generation space contains two separate areas: the eden space and the survivor space. We won’t dive into the garbage collection process in this chapter, but we’ll briefly mention it and what it is to explain how objects are promoted between spaces. Visualization of the heap and the different areas will be added to provide clarity regarding the heap space’s details. The content of this chapter is necessary to understand the garbage collection algorithms that will be discussed in the next chapter.

Chapter 4, Freeing the Memory with Garbage Collection, dives into the deallocation of the objects on the heap. Deallocation of the memory is necessary in order for an application to keep running. Without the ability to free memory, we could allocate it only once and eventually we’d run out of memory. In this chapter, we are dealing with when objects on the heap space are eligible for garbage collection and what phases the garbage collector goes through. We’ll end with a discussion on the different implementations of the garbage collector. We’ll make this as visual as possible to increase understanding.

Chapter 5, Zooming in on the Metaspace, touches upon the Metaspace, which is used by the JVM for class metadata and for example static variables. This metadata gets stored when the classes are loaded. We’ll describe the class loading process and how memory is allocated. The releasing of Metaspace memory is a bit different from the releasing of heap memory. This process will be described here as well.

Chapter 6, Configuring and Monitoring the Memory Management of the JVM, explains how to get started with JVM tuning. First, we’ll describe what JVM tuning is and who needs it. There are several metrics that are relevant for the tuning of the JVM when it comes to memory management. We’ll examine these metrics and how to obtain them. We’ll end with the actual tuning and adjusting of the configuration of the JVM and how to use profiling to get insights into the effects of the tuning.

Chapter 7, Avoiding Memory Leaks, deals with how to use the memory well and how to spot and solve memory leaks. Whenever objects are held in memory that are no longer needed, we get memory leaks. In the beginning, this can seem harmless, but over time it will slow down an application and the application will require a restart in order to function properly again. In this chapter, we’re going to make sure that the reader understands memory leaks and knows how to spot them. We’ll end with very common mistakes that lead to memory leaks and how to avoid them.