public class ObjectSizeCalculator extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
ObjectSizeCalculator.MemoryLayoutSpecification
Describes constant memory overheads for various constructs in a JVM implementation.
|
Constructor and Description |
---|
ObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification)
Creates an object size calculator that can calculate object sizes for a given
memoryLayoutSpecification . |
ObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification,
Set<Class<?>> constSizeClass)
Creates an object size calculator that can calculate object sizes for a given
memoryLayoutSpecification . |
ObjectSizeCalculator(ObjectSizeCalculator calc)
Creates an object size calculator based on a previous one.
|
Modifier and Type | Method and Description |
---|---|
long |
calculateObjectSize(Object obj)
Given an object, returns the total allocated size, in bytes, of the object
and all other objects reachable from it.
|
static long |
getObjectSize(Object obj)
Given an object, returns the total allocated size, in bytes, of the object
and all other objects reachable from it.
|
static long |
getObjectSize(Object obj,
Set<Class<?>> constantClasses)
Given an object, returns the total allocated size, in bytes, of the object
and all other objects reachable from it.
|
public ObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification)
memoryLayoutSpecification
.memoryLayoutSpecification
- a description of the JVM memory layoutpublic ObjectSizeCalculator(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification, Set<Class<?>> constSizeClass)
memoryLayoutSpecification
.memoryLayoutSpecification
- a description of the JVM memory layoutconstSizeClass
- constant sized classespublic ObjectSizeCalculator(ObjectSizeCalculator calc)
calc
- previous calculatorpublic static long getObjectSize(Object obj) throws UnsupportedOperationException
UnsupportedOperationException
;obj
- the object; can be null. Passing in a Class
object 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.UnsupportedOperationException
- if the current vm memory layout cannot be detectedpublic static long getObjectSize(Object obj, Set<Class<?>> constantClasses) throws UnsupportedOperationException
UnsupportedOperationException
;obj
- the object; can be nullconstantClasses
- a set of classes that are considered constant sizedUnsupportedOperationException
public long calculateObjectSize(Object obj)
obj
- the object; can be null. Passing in a Class
object 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.Copyright © 2023. All Rights Reserved.