I have a class, and in it - the field. How to find out the size of an object initialized by this class?

  • your heading does not match the question body, since simple types have no class. You are interested in the amount of memory occupied by an instance of a class - jmu

2 answers 2

The idea is this: measure the amount of memory used before creating an instance of a class ( usedMemoryBefore ), measure it after working with an instance ( usedMemoryAfter ), and our answer is their difference ( usedMemory ).

 Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); //создание и работа с экземпляром класса long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); long usedMemory = usedMemoryAfter - usedMemoryBefore; 
  • one
    It is necessary to understand that this is an unwarranted method and you need to do several measurements. Otherwise, you can get any random result. Of course, you need to create many copies and divide by their number in order to find out how much space is occupied. - cy6erGn0m

If you want to measure more accurately and not use the agent for this, but we need to count for each field type. Such a method will be accurate but rather slow if the object has too many objects.

 import java.lang.reflect.*; import java.util.*; /** * Created by TS on 7/21/2016. */ public class ObjectSizeCalculator { private static final int REFERENCE_SIZE; private static final int HEADER_SIZE; private static final int LONG_SIZE = 8; private static final int INT_SIZE = 4; private static final int BYTE_SIZE = 1; private static final int BOOLEAN_SIZE = 1; private static final int CHAR_SIZE = 2; private static final int SHORT_SIZE = 2; private static final int FLOAT_SIZE = 4; private static final int DOUBLE_SIZE = 8; private static final int ALIGNMENT = 8; static { try { if (Helpers.getEnvironmentVariable("java.vm.name", "Java HotSpot(TM) 64-Bit Server VM" ).toString().contains("64")) { REFERENCE_SIZE = 8; HEADER_SIZE = 16; } else { REFERENCE_SIZE = 4; HEADER_SIZE = 8; } } catch (Exception ex) { ex.printStackTrace(); throw new AssertionError(ex); } } public static long sizeOf(Object o) throws IllegalAccessException { return sizeOf(o, new HashSet()); } private static long sizeOf(Object o, Set visited) throws IllegalAccessException { if (o == null) { return 0; } ObjectWrapper objectWrapper = new ObjectWrapper(o); if (visited.contains(objectWrapper)) { //We have reference graph with cycles. return 0; } visited.add(objectWrapper); long size = HEADER_SIZE; Class clazz = o.getClass(); if (clazz.isArray()) { if (clazz == long[].class) { long[] objs = (long[]) o; size += objs.length * LONG_SIZE; } else if (clazz == int[].class) { int[] objs = (int[]) o; size += objs.length * INT_SIZE; } else if (clazz == byte[].class) { byte[] objs = (byte[]) o; size += objs.length * BYTE_SIZE; } else if (clazz == boolean[].class) { boolean[] objs = (boolean[]) o; size += objs.length * BOOLEAN_SIZE; } else if (clazz == char[].class) { char[] objs = (char[]) o; size += objs.length * CHAR_SIZE; } else if (clazz == short[].class) { short[] objs = (short[]) o; size += objs.length * SHORT_SIZE; } else if (clazz == float[].class) { float[] objs = (float[]) o; size += objs.length * FLOAT_SIZE; } else if (clazz == double[].class) { double[] objs = (double[]) o; size += objs.length * DOUBLE_SIZE; } else { Object[] objs = (Object[]) o; for (int i = 0; i < objs.length; i++) { size += sizeOf(objs[i], visited) + REFERENCE_SIZE; } } size += INT_SIZE; } else { Field[] fields = o.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { continue; } fields[i].setAccessible(true); String fieldType = fields[i].getGenericType().toString(); if (fieldType.equals("long")){ size += LONG_SIZE; }else if (fieldType.equals("int")){ size += INT_SIZE; }else if (fieldType.equals("byte")){ size += BYTE_SIZE; }else if (fieldType.equals("boolean")){ size += BOOLEAN_SIZE; }else if (fieldType.equals("char")){ size += CHAR_SIZE; }else if (fieldType.equals("short")){ size += SHORT_SIZE; }else if (fieldType.equals("float")){ size += FLOAT_SIZE; }else if (fieldType.equals("double")){ size += DOUBLE_SIZE; }else{ size += sizeOf(fields[i].get(o), visited) + REFERENCE_SIZE; } } } if ((size % ALIGNMENT) != 0) { size = ALIGNMENT * (size / ALIGNMENT) + ALIGNMENT; } return size; } private static final class ObjectWrapper { private Object object; public ObjectWrapper(Object object) { this.object = object; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if ((obj == null) || (obj.getClass() != ObjectWrapper.class)) { return false; } return object == ((ObjectWrapper) obj).object; } @Override public int hashCode() { int hash = 3; hash = 47 * hash + System.identityHashCode(object); return hash; } } }