Package io.debezium.util
Class ObjectSizeCalculator
java.lang.Object
io.debezium.util.ObjectSizeCalculator
Contains utility methods for calculating the memory usage of objects. It
only works on the HotSpot JVM, and infers the actual memory layout (32 bit
vs. 64 bit word size, compressed object pointers vs. uncompressed) from
best available indicators. It can reliably detect a 32 bit vs. 64 bit JVM.
It can only make an educated guess at whether compressed OOPs are used,
though; specifically, it knows what the JVM's default choice of OOP
compression would be based on HotSpot version and maximum heap sizes, but if
the choice is explicitly overridden with the -XX:{+|-}UseCompressedOops command line
switch, it can not detect
this fact and will report incorrect sizes, as it will presume the default JVM
behavior.
- Author:
- Attila Szegedi
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate classprivate static classstatic interfaceDescribes constant memory overheads for various constructs in a JVM implementation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final com.google.common.cache.LoadingCache<Class<?>,ObjectSizeCalculator.ClassSizeInfo> private final intprivate final intprivate final intprivate longprivate final int -
Constructor Summary
ConstructorsConstructorDescriptionObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification) Creates an object size calculator that can calculate object sizes for a givenmemoryLayoutSpecification. -
Method Summary
Modifier and TypeMethodDescriptionlongGiven an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.(package private) void(package private) static ObjectSizeCalculator.MemoryLayoutSpecificationstatic longgetObjectSize(Object obj) Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.private static longgetPrimitiveFieldSize(Class<?> type) private voidincreaseByArraySize(int length, long elementSize) (package private) voidincreaseSize(long objectSize) (package private) static longroundTo(long x, int multiple) private voidprivate voidvisitArray(Object array)
-
Field Details
-
arrayHeaderSize
private final int arrayHeaderSize -
objectHeaderSize
private final int objectHeaderSize -
objectPadding
private final int objectPadding -
referenceSize
private final int referenceSize -
superclassFieldPadding
private final int superclassFieldPadding -
classSizeInfos
private final com.google.common.cache.LoadingCache<Class<?>,ObjectSizeCalculator.ClassSizeInfo> classSizeInfos -
alreadyVisited
-
pending
-
size
private long size
-
-
Constructor Details
-
ObjectSizeCalculator
public ObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification) Creates an object size calculator that can calculate object sizes for a givenmemoryLayoutSpecification.- Parameters:
memoryLayoutSpecification- a description of the JVM memory layout.
-
-
Method Details
-
getObjectSize
Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it. Attempts to to detect the current JVM memory layout, but may fail withUnsupportedOperationException;- Parameters:
obj- the object; can be null. Passing in aClassobject doesn't do anything special, it measures the size of all objects reachable through it (which will include its class loader, and by extension, all other Class objects loaded by the same loader, and all the parent class loaders). It doesn't provide the size of the static fields in the JVM class that the Class object represents.- Returns:
- the total allocated size of the object and all other objects it retains.
- Throws:
UnsupportedOperationException- if the current vm memory layout cannot be detected.
-
calculateObjectSize
Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.- Parameters:
obj- the object; can be null. Passing in aClassobject doesn't do anything special, it measures the size of all objects reachable through it (which will include its class loader, and by extension, all other Class objects loaded by the same loader, and all the parent class loaders). It doesn't provide the size of the static fields in the JVM class that the Class object represents.- Returns:
- the total allocated size of the object and all other objects it retains.
-
visit
-
visitArray
-
increaseByArraySize
private void increaseByArraySize(int length, long elementSize) -
enqueue
-
increaseSize
void increaseSize(long objectSize) -
roundTo
static long roundTo(long x, int multiple) -
getPrimitiveFieldSize
-
getEffectiveMemoryLayoutSpecification
-