Appendix E. Memory management in Java apps

 

In this appendix, we discuss how the Java Virtual Machine (JVM) manages the memory of a Java app. Some of the most challenging problems you’ll have to investigate in Java apps are related to the way the apps manage memory. Fortunately, we can use several techniques to analyze such problems and find their root causes, with minimal time invested. But to benefit from those techniques, you first need to know at least some basics about how a Java app manages its memory.

An app’s memory is a limited resource. Even if today’s systems can offer a large amount of memory for an app to use during its execution, we still need to be careful with how an app spends this resource. No system can offer unlimited memory as a magical solution (figure E.1). Memory issues lead to performance problems (the app becomes slow, it’s more costly to deploy, it starts more slowly, etc.) and sometimes can even bring the entire process to a complete stop (e.g., in the case of an OutOfMemoryError).

E.1 How the JVM organizes an app’s memory

E.2 The stack used by threads to store local data

E.3 The heap the app uses to store object instances

E.4 The metaspace memory location for storing data types