Java Basics

Java heap space – Everything you need to know

In this tutorial we will discuss about Java’s heap space. To begin with, the Java Virtual Machine (JVM) is the execution component of the Java Runtime Environment (JRE) that executes the bytecode residing in a Java .class file. While executing an application, new objects are created. The heap space is where all new objects are stored, while the application is being executed by the JVM.

When an application requires the creation of a new object, the JVM is responsible for allocating the necessary space. Thus, the JVM allocates a contiguous area of heap memory, in order for the new object to be stored. The Java heap space is generally located at the bottom of the address space and move upwards, as new objects are being created.

Each object in the heap that is referenced by any other object is called live and remains in the heap, as long as that condition is being held. Once an object is not referenced by any other object, it can be cleared out of the heap, in order for the JVM to reclaim and reuse that space. The execution thread that is responsible to clear the heap space is the Garbage Collector. The task of the Garbage Collector is to find all objects that are not referenced at all and reclaim their space. Usually, a Garbage Collector is being executed periodically by the JVM, in order for new space to be created.

The heap space is divided into generations:

  • The young generation: The young generation stores all short-lived objects that are created by an application and are cleared after a small number of garbage collections.
  • The old generation: The old generation holds all objects that have survived a number of minor garbage collections. These objects are moved from the young generator to the old one by the Garbage Collector.
  • The permanent generation (or permgen): The permanent generation is used for class definitions and the associated metadata. However, the permanent generation is not considered actual part of the heap space.

In a 32-bit architecture system, the maximum heap space of a Java application cannot exceed the 4 GBs. A Java process consists of many spaces and its allocated space cannot exceed the 4GBs. Specifically, a Java process consists of the following spaces:

  • Loaded libraries, including .jar and .class files.
  • Structures for manipulating the heap space.
  • Thread stacks.
  • Generated code by the JIT compiler.
  • Native memory of the application.

Getting information about the heap space

A Java application is able to gather information about the size of the heap space, the available heap space and the maximum size of the heap space, using the Runtime class. A sample main method that prints the aforementioned information is shown below:

HeapSizeExample.java:

public class HeapSizeExample {

     public static void main(String[] args) {
          System.out.println("Total Memory (in bytes): " + Runtime.getRuntime().totalMemory());
          System.out.println("Free Memory (in bytes): " + Runtime.getRuntime().freeMemory());
          System.out.println("Max Memory (in bytes): " + Runtime.getRuntime().maxMemory());
     }
}

The execution of the main method in my personal computer prints the following information:

Total Memory (in bytes): 48234496
Free Memory (in bytes): 47730992
Max Memory (in bytes): 716177408

Notice that the results of the execution may differ from computer to computer and highly depend on the platform you execute the application. From example, the results may differ if you execute the same application from the Windows command line and from the Eclipse IDE.

Tuning the Java Virtual Machine

The size of the available heap space can be configured while starting the JVM. The most common configurations are shown below:

-Xms<size>

Set the initial size of the Heap.

-Xmx<size>

Set the maximum size of the Heap.

Notice that you cannot change the size of the Java heap space during runtime. The parameters are specified only while starting the JVM. Also, the maximum heap size must be greater or equal than the heap’s initial size.

For more information about tuning the Java Heap space please visit this link.

Memory leaks

The Garbage Collector reclaims those objects that are not referenced by any other object, in order to free space. If an object is being referenced at least one time, then the Garbage Collector will not reclaim that object.

A memory leak can be created when there are objects inaccessible by the running code but still reside in main memory and cannot be collected. One common indication of a memory leak is the OutOfMemoryError. However, an OutOfMemoryError is not thrown only in cases of a memory leak.

This error is thrown by the JVM when there is no available space in the heap, in order to create a new Object. Moreover, the Garbage Collector cannot reclaim any Object from the heap and create free space and thus, the execution of the application is aborted. An OutOfMemoryError can also be thrown by the code of a native library, when the allocation cannot be satisfied.

The Java Development Kit (JDK) provides tools that help you acquire the heap dump of an application and analyze each heap dump. The jmap is a sample example of a memory map tool, while jhat is an example of a Java heap analysis tool.

 
This was a tutorial about Java’s heap space.

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button