Java size of objects

Wednesday, 15 June 2011 10:56

Size of objects in Java

Size of Objects in Java

Since Java purposefully hides many aspects of memory management, discovering how much memory your objects consume can be very helpfull especially when you are working with large about of memory.�The following tutorial will provide you with a small tool to help you to calculate the memory consumption of your objects.

Utillity class to calculate object size

To achieve our goal we will develop an interface in charge to instantiate the object we want to measure the size. We will then�measure heap size differences before and after several objects have been allocated and divide the difference by the number of times we have instantiated the subject class.

Let's define our Instantiator interface :

size of a java object instance


public interface Instantiator {
	Object execute();
}

Our sizeOf utility class definition :

Utility class to determine size of a Java Object


public class SizeOfUtil {

	private static final Runtime runtime = Runtime.getRuntime();
	private static final int OBJECT_COUNT = 100000;

	/**
	 * Return the size of an object instantiated using the instantiator
	 * 
	 * @param instantiator
	 * @return byte size of the instantiate object
	 */ 	 
	static public int execute(Instantiator instantiator) {
		runGarbageCollection();
		usedMemory();
		Object[] objects = new Object[OBJECT_COUNT + 1];
		long heapSize = 0;
		for (int i = 0; i < OBJECT_COUNT + 1; ++i) {
			Object object = instantiator.execute();
			if (i > 0)
				objects[i] = object;
			else {
				object = null;
				runGarbageCollection();
				heapSize = usedMemory();
			}
		}
		runGarbageCollection();
		long heap2 = usedMemory(); // Take an after heap snapshot:
		final int size = Math.round(((float) (heap2 - heapSize)) / OBJECT_COUNT);
		
		for (int i = 1; i < OBJECT_COUNT + 1; ++i)
			objects[i] = null;
		objects = null;
		return size;
	}

	private static void runGarbageCollection() {
		for (int r = 0; r < 4; ++r){
			long usedMem1 = usedMemory();
			long usedMem2 = Long.MAX_VALUE;
			for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
				runtime.runFinalization();
				runtime.gc();
				Thread.yield();
				usedMem2 = usedMem1;
				usedMem1 = usedMemory();
			}
		}
	}

	private static long usedMemory() {
		return runtime.totalMemory() - runtime.freeMemory();
	}
}

Java Size of objects usage

We have now an instantiator in charge of creating instances of the Java object we want to determine the size and the size of utility class to calculate it using the Runtime class.

In the following example we will define the size of an Integer object :

Using SizeOf to define object size


	public void sizeOfInteger(){
		int size = SizeOfUtil.execute(new Instantiator(){
			@Override public Object execute() {
				return new Integer (3);
			}
		});
		System.out.println(Integer.class.getSimpleName() + " size = " + size + " bytes");
	}

The result is

Integer size = 16 bytes

Conclusion

You hold now all the cards to define your own evaluation for your objects. Depending on the size of your objects or object graph you want to test it can be usefull to change the OBJECT_COUNT variable in the SizeOfUtil class to reduce the time spent to calculate your object size.

Tags: java , objects , class , public , object , size , define , calculate , instantiator

Add comment


Security code
Refresh