Java Memory Management - Maaike van Putten - E-Book

Java Memory Management E-Book

Maaike van Putten

0,0
26,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 164

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Java Memory Management

A comprehensive guide to garbage collection and JVM tuning

Maaike van Putten

Seán Kennedy

BIRMINGHAM—MUMBAI

Java Memory Management

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Gebin George

Publishing Product Manager: Kunal Sawant

Senior Editor: Rounak Kulkarni

Technical Editor: Jubit Pincy

Copy Editor: Safis Editing

Language Support Editor: Safis Editing

Project Coordinator: Deeksha Thakkar

Proofreader: Safis Editing

Indexer: Hemangini Bari

Production Designer: Shankar Kalbhor

Developer Relations Marketing Executive: Sonakshi Bubbar

Business Development Executive: Kriti Sharma

First published: November 2022

Production reference: 1281022

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80181-285-6

www.packt.com

To my students and my teachers.

– Maaike van Putten

To my wife, Maria, and my daughters, Emily, Miriam, and Lilian.

– Seán Kennedy

Contributors

About the authors

Maaike van Putten is an experienced software developer and trainer with a passion for software development and helping others to get to the next level in their career. Some of her favorite languages are Java, JavaScript, and Python. She participates as a developer in software development projects and teaches a lot of training courses, ranging from IT for beginners to advanced topics for senior software developers. She also loves to create online content to help a larger audience, and she does so for diverse platforms such as Pluralsight, LinkedIn Learning, and Udemy.

I want to thank my students, my teachers, my friends, and my family for always supporting me.

Seán Kennedy is a university lecturer with over 20 years of experience in teaching. He has a PhD in IT and is Oracle-certified in Java at the Professional level (OCP). In his daily work, he teaches Java on a bespoke master’s program for a highly regarded software company. He has a YouTube channel called Let’s Get Certified that teaches Java at all levels and prepares candidates for Java certification. He also has similar courses on Udemy. Outside of work, he enjoys tennis, walking, nature, reading, and TV.

I want to thank the people who have always supported me, especially my wife, Maria, and my late parents.

About the reviewers

Himanshu Agrawal is an industry veteran with close to 13 years of experience in designing and implementing solutions in JEE technologies. Currently, he works as an Associate Consultant with CGI, with whom he has been associated for close to 13 years. Himanshu specializes in niche technical areas like JVM, Multithreading, TLS, and Apache HTTPD. He is an Oracle Certified Java Dev, Oracle Certified Web Component Dev and certified by Google Cloud in Architecting with Google Kubernetes Engine. Moreover, he is a Certified SAFe 5 Practitioner and has done certificate courses from top universities like Harvard (via HarvardX) and MIT (via MITx). Himanshu extends his technical expertise to teams primarily in BFSI and Telecom sectors.

Silviu Turuga is an IT professional who has worked for almost 20 years for different companies around the world. During this time, he has performed various roles, from technical support, software development, and technical team lead to DevOps and DevSecOps. He has used many different programming languages and frameworks. Silviu is an Oracle Certified Professional for Java 8. He loves to see everything as a challenge that has to have a solution. He is an active team player, willing to share his knowledge with others, and also enjoys mentoring. During his free time, he likes to perform home automation and play with IoT devices.

Table of Contents

Preface

1

Different Parts of the Java Memory

Technical requirements

Understanding computer memory and Java memory

Computer memory

Accessing the main memory

Overview of the main memory

Java memory and the JVM

The JVM

Memory management and the JVM

Memory management before Java

Understanding the JVM’s components for memory management

Runtime data area

Creating variables in Java

Primitives and reference types

Storing variables on the stack

Current frame and current method

Elements of the frame

Values on the stack

Primitives and wrapper classes

Creating objects in Java

Storing objects on the heap

Exploring the Metaspace

Summary

2

Primitives and Objects in Java Memory

Technical requirements

Understanding primitives on the stack and heap

Storing objects on the heap

References

Objects

Understanding the differences between references and objects

Managing object references and security

Inspecting the escaping references issue

Summary

3

Zooming in on the Heap Space

Exploring the different generations on the heap

Garbage collection roots

Learning how the spaces are used

Understanding the minor garbage collection algorithm

Demonstrating the minor garbage collection algorithm in action

Summary

4

Freeing Memory with Garbage Collection

Technical requirements

Being eligible for GC

Marking by the garbage collector

Stop-the-world

Reference counting and islands of isolation

Sweeping by the garbage collector

Normal sweeping

Sweeping with compacting

Sweeping with copying

Exploring GC implementations

Generational GC

Serial GC

Parallel GC

CMS GC

G1 GC

Z GC

Monitoring GC

Summary

5

Zooming in on the Metaspace

JVM usage of the Metaspace

Class loading

Releasing Metaspace memory

Metaspace runs out of memory

Metaspace size exceeds a JVM-set threshold

Garbage collection of the Metaspace

Summary

6

Configuring and Monitoring the Memory Management of the JVM

Technical requirements

The basics of JVM tuning for memory management

Obtaining relevant metrics for memory management

Well-functioning memory

Normal latency

Level of throughput

Profiling Java applications

Profiling with jstat and jmap

Profiling with VisualVM

Tuning the configurations of the JVM

Tuning the heap size and thread stack size

Logging low memory

Tuning the Metaspace

Garbage collection tuning

7

Avoiding Memory Leaks

Technical requirements

Understanding memory leaks

Spotting memory leaks

Heap memory footprint

Garbage collector activity

Heap dump

Avoiding memory leaks

Common pitfalls and how to avoid them

Summary

Index

Other Books You May Enjoy

1

Different Parts of the Java Memory

Do you know the phenomenon of having to restart an application to boost the performance of that application? If so, you may have experienced the outcome of poor memory management: the memory getting full and the application slowing down. This is not always why applications slow down – other causes such as processing data from a server or a bottleneck in the network, among other things, play a role – but memory management problems are a usual suspect of degrading application performance.

You’ve probably heard of memory in the field of computer science before. That makes sense because computers have memory and they use this memory to store and access data while running programs (which in their turn are data too!).

So, when does an application use memory? Well, for example, let’s say you’d like to run an application that is going to process a huge video file. If you do this with your activity monitoring application (for example, Activity Monitor on macOS or Task Manager on Windows) open, you’ll see that the used memory increases once you open the application and load the video. Memory is a finite resource on your computer and once your computer runs out of it, it becomes slow.

There are many ways to improve the performance of an application. A deeper understanding of how exactly this memory works is one of the ways that could help you improve the performance of your applications. Memory that is used efficiently by using good practices in coding is going to boost the performance of your application. So, coding well and being mindful of how the memory works should always be the first way to achieve high performance when it comes to memory management. There is another way in which Java memory management can be influenced and that is by configuring the Java Virtual Machine (JVM), which takes care of the Java memory. This is something that we’ll cover in Chapter 6 when we’re ready for it.

The efficient handling of Java memory is of crucial importance for the performance of a Java application. In Java, this is especially the case because it comes with expensive processes such as garbage collection, which again, we’ll see later after gaining enough basic knowledge to comprehend it.

Memory management is