appendix-e

appendix E  Memory management in Java apps

 

This appendix discusses 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 problems 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).

Figure E.1 An app’s memory is a limited resource. There’s no magical solution that allows us to allocate infinite memory to an app. When building apps, we need to treat memory consumption with consideration and avoid spending it for no reason. Apps may sometimes have memory problems. If a certain capability uses too much memory, it can cause performance problems or even a complete failure. You need to be ready to find the causes of such problems and solve them properly.

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