Posted In: Java Core

How to determine the size of an object in Java

JDK 5 provides Instrumentation interface which can be used find Object Size

public interface Instrumentation
This class provides services needed to instrument Java programming language code. Instrumentation is the addition of byte-codes to methods for the purpose of gathering data to be utilized by tools. Since the changes are purely additive, these tools do not modify application state or behavior. Examples of such benign tools include monitoring agents, profilers, coverage analyzers, and event loggers.

Instrumentation set up requires entry to manifest file Premain-Class: ObjectAgent and use public static void premain(String agentArgs, Istrumentation instrumentation).

There is little easy way to do it. Use memory-measurer

Run class with javaagent VM argument




import java.util.ArrayList;
import java.util.List;

import objectexplorer.MemoryMeasurer;
import objectexplorer.ObjectGraphMeasurer;

public class ObjectSize {

	public static void main(String[] args) throws Exception {
		new ObjectSize().size();

	private void size() {
		List<Pojo1> list = new ArrayList<Pojo1>();
		list.add(new Pojo1());
		list.add(new Pojo1());
		list.add(new Pojo1());
		list.add(new Pojo1());


by , on August 24th, 2017

  • Categories