Browse Prior Art Database

Measuring the Size of a Java Object Graph at Runtime

IP.com Disclosure Number: IPCOM000024638D
Original Publication Date: 2004-Apr-02
Included in the Prior Art Database: 2004-Apr-02
Document File: 5 page(s) / 136K

Publishing Venue

IBM

Abstract

Determining the size of a Java object is not a straightforward process as it involves traversing the object graph. Many applications do require information about object sizes such as profilers, compilers, and other programs in which memory management is a consideration. We describe in this article a method for computing the size of a runtime Java object. This method has been implemented and is available as a Java package.

This text was extracted from a PDF file.
At least one non-text object (such as an image or picture) has been suppressed.
This is the abbreviated version, containing approximately 27% of the total text.

Page 1 of 5

Measuring the Size of a Java Object Graph at Runtime

Introduction

This idea came about when we got involved in a project in which we had to analyze the memory footprint of a middleware application that we were developing. The problem was that our application uses too much memory from the Java heap that it slows down its performance due to frequent garbage collection. The task was not only to determine how much memory is actually being used but which objects are actually using them, and for these objects, how much each individual objects are actually consuming. Once this information becomes available, the next goal is to find out why these objects need those memory. The other purpose is, given that we know the size of an object, to be able to predict how much total heap will be needed if we anticipate that these types of objects will have a certain number of instances. In other words, it allows us to plan ahead or make an intelligent decision on how to set our heap sizes. So the first thing we did was to look for a tool that we can use to programmatically check the size of an object and to our surprise, there was no such tool available. This was how the idea of coming up with our own method and implementing it came to life. In this article, we will describe the method, its implementation and how it can be used.

The Java Runtime Object

The first requirement for developing such a tool is getting an intimate understanding of how objects in the Java runtime are actually represented and implemented. A Java object is allocated either from the Java heap or thread stack. In most implementations, the thread stack itself is part of the Java heap. The methods of an object, however, are allocated in a special portion of the Java Virtual Machine called the Method Area. Every object instance of a class share the same methods in the method area. Therefore, no additional memory is allocated for the methods in an object instance. This concept is similar to processes where shared libraries only occupy a single space in memory and processes execute the same code in the shared libraries. So the question is: what do we mean exactly by "size of an object" ? What constitutes a Java object ?

Yes, we need to clarify what we meant by the size of an object. What constitutes an object that we consider part of the object's footprint ? A runtime Java object is not just a simple group structure or record of fields or attributes. A Java object is actually much more complicated than that because it may consist of a graph of objects. This is depicted in Figure


1.

1

Page 2 of 5

attributes

methods

attributes

methods

   attributes methods

 attributes methods

attributes

methods

attributes

methods

Object 1

Object 2

Figure 1: A runtime Java Object is a graph of Objects

In Figure 1, our object of interest is Object 1. We know that an object consists of attributes where an attribute can be of primitive type (int, short, char, double, boolean, byte, float), or a reference to an...