Memory Management General Rules
This section describes some general rules used by JVM to manage memory.
We know that one strength of JVM (Java Virtual Machine) is that it performs automatic memory
management for us. But this also could also give us headaches when JVM throws OutOfMemoryError at us,
because we don't have much direct control on how memory is managed.
The only thing we can is to learn how JVM memory management works, write Java code better aligned
with the JVM memory management design, and play with JVM memory management parameters for better result.
Here are my understandings of HotSpot memory management and suggestions on to avoid OutOfMemoryError exceptions:
- The main challenge of JVM memory management is on the Heap data area,
which dynamically allocates and deallocates data objects (instances of classes and arrays).
Other runtime data areas, Method Area, Direct Memory, PC Registers, JVM Stacks and Native Method Stacks
are very easy to manage.
- When a JVM is started, it will ask the operating system to reserve a chunk of memory
and use it as the Heap data area with a minimum size.
The JVM will use Heap space to store some JVM system objects
and give the rest of the Heap space to your Java application.
- HotSpot supports 2 options to control the minimum size and the maximum size of the Heap space: "-Xms" and "-Xmx".
- Configure the JVM with as much as possible memory from the operating system to run your Java application if needed.
For example, on a 32-bit Windows system, we can run a JVM with up to about 1.5 GB heap memory: "-Xmx1400m".
- When a new data object is created each time, the JVM needs to find enough Heap space and allocate it to the object.
- Reuse objects as much as possible to reduce the JVM work load on allocating and removing objects.
This is why some applications use Object Pooling.
- When a object becomes unreferenced or no longer used by the application, it will be a candidate
to be removed by the JVM to free up more memory in the next round of garbage collection process.
- Remove references to objects as soon as possible to allow the JVM to remove them sooner.
- Use totalMemory() and freeMemory() methods of the current Runtime instance to monitor the Heap memory usage.
- "Slow" down the application to reduce the memory usage. This is trading time for memory.
For example, if your application handles work loads with concurrent multiple threads,
use less threads and pub work loads into waiting queues will reduce the memory usage.
- Use concurrent garbage collectors if you want to ensure fast response and you have multiple CPUs.
- Read technical documents like
Memory Management in the Java HotSpot Virtual Machine
to understand how JVM actually does objects allocation and deallocation.
- Play with memory management options provided by the JVM to see the impact on your Java application.
- Using JVM troubleshooting tool to profile, dump, analyze, and do fine tuning on the memory usage pattern of your Java application.
The following sections will provide tutorial examples to demonstrate some of these suggestions.
Last update: 2014.
Table of Contents
About This Book
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.0 on Windows
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
Sun's JVM - Java HotSpot VM
JRockit JVM 28.2.7 by Oracle Corporation
JVM Runtime Data Areas
►Memory Management and Garbage Collectors
►Memory Management General Rules
Java Exception: "java.lang.OutOfMemoryError: Java heap space"
OutOfMemoryError Comparison of HotSpot and JRockit
Garbage Collection Demonstration
JVM Memory Manager - Garbage Collector
Generational Garbage Collection in HotSpot
Young Generation Collection - Minor Collection
Tenured Generation Collection - Full Collection
HotSpot Default Garbage Collector - Serial Collector
"-XX:+PrintGCDetails" - Garbage Collection Logging
GC Log Messages on GarbageCollection.java
Serial, Parallel, Concurrent, and Regionalized Collectors
Parallel Collector GC Log Message Format
Parallel Compacting Collector GC Log Message Format
Concurrent Mark-Sweep Collector GC Log Message Format
Garbage First GC Log Message Format
Garbage Collection Tests
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
CDS (Class Data Sharing)
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations
PDF Printing Version