Preface

Understanding how Java memory works can be of great benefit to your Java coding and application management. It makes it easier to visualize what is going on with object composition and what happens with object allocation and deallocation, combined with object composition. As you are probably aware, object composition is where objects contain other objects. For example, a Person class specifies a property of type Address, which also happens to be a class. Knowing how this all works in memory makes it easier to know what steps to take to get to a certain data field.

Also, the concept of static and accessing the instance using the this keyword will be so much easier to visualize and understand completely when you understand how the memory of Java works. Without understanding how Java memory works, it is impossible to truly grasp the concepts of static and this.

Another advantage of understanding Java memory well is that the difference between using a primitive or a class reference as an argument makes a lot more sense all of a sudden. This also helps with understanding the immutability and mutability of objects.

More complex topics will make more sense too, such as concurrency, one of my personal favorites. This is when multiple things are happening at the same time in your application (multithreading). It’s possible that you haven’t worked with it yet, but as a Java developer, you’ll have to one day. Understanding Java memory makes it easier to understand certain aspects of concurrency, especially data access. Another complex topic that will be easier to understand is garbage collection. This is of crucial importance for performance since it’s a very expensive process, and you want to need it as little as possible and optimize it as much as possible.

Everything that you are probably using on a daily basis already will become clearer when you understand better how Java memory works.

Who this book is for

This book is for all sorts of Java professionals. It doesn’t really matter whether you’re a junior or senior developer, a DevOps engineer, a tester, or the system admin of a Java application. If you currently do not have an in-depth knowledge of Java memory, garbage collection, and/or JVM tuning, this book will help you to take your Java skills to the next level.

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.

To get the most out of this book

This book assumes Java 8 or later. A particular operating system or IDE is not mandatory. If you plan to run the examples yourself, then VisualVM, the visual tool for monitoring Java applications in memory, would be useful.

If you currently have nothing installed on your system, the following setup will suffice:

  • JDK 8 or later (Oracle’s JDK or OpenJDK)
  • IntelliJ IDEA(community edition is good enough) or Eclipse
  • VisualVM

Software/hardware covered in the book

Operating system requirements

Java 8+

Windows, macOS, or Linux

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/B18762_Java-Memory-Management. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/OeQqF.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “For example, int x; defines (creates) a primitive variable x which is of (the primitive) type int.”

A block of code is set as follows:

Object o = new Object();
System.out.println(o);
o = null;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

Object o = new Object();
System.out.println(o);
o = null;

Any command-line input or output is written as follows:

java.lang.Object@4617c264

Bold: Indicates a new term, an important word, or words that you see on screen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “There is a Create another option beside the Create button.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Java Memory Management, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a Free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781801812856

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.125.69