Top Description Inners Fields Constructors Methods
com.carrotsearch.sizeof

public final Class RamUsageEstimator

extends Object
Class Inheritance
Imports
java.lang.management.ManagementFactory, java.lang.reflect.Array, .Field, .InvocationTargetException, .Method, .Modifier, java.text.DecimalFormat, .DecimalFormatSymbols, java.util.*

Estimates the size (memory representation) of Java objects.
See Also
sizeOf(Object), shallowSizeOf(Object), shallowSizeOfInstance(Class)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
RamUsageEstimator.ClassCache

Cached information about a given class.

private static class
private static class
public static enum
RamUsageEstimator.JvmFeature

JVM diagnostic features.

Field Summary

Modifier and TypeField and Description
public static final String
JVM_INFO_STRING

JVM info string for debugging and reports.

public static final int
NUM_BYTES_ARRAY_HEADER

Number of bytes to represent an array header (no content, but with alignments).

public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
NUM_BYTES_OBJECT_ALIGNMENT

A constant specifying the object alignment boundary inside the JVM.

public static final int
NUM_BYTES_OBJECT_HEADER

Number of bytes to represent an object header (no fields, no alignments).

public static final int
NUM_BYTES_OBJECT_REF

Number of bytes this jvm uses to represent an object reference.

public static final int
private static final Method
objectFieldOffsetMethod

A handle to sun.misc.Unsafe#fieldOffset(Field).

public static final long
ONE_GB

One gigabyte bytes.

public static final long
ONE_KB

One kilobyte bytes.

public static final long
ONE_MB

One megabyte bytes.

private static final Map<Class<?>, Integer>
primitiveSizes

Sizes of primitive classes.

private static final EnumSet<RamUsageEstimator.JvmFeature>
supportedFeatures

All the supported "internal" JVM features detected at clinit.

private static final Object
theUnsafe

A handle to sun.misc.Unsafe.

Constructor Summary

AccessConstructor and Description
private
RamUsageEstimator()

No instantiation.

Method Summary

Modifier and TypeMethod and Description
private static long
adjustForField(long sizeSoFar, final Field f)

This method returns the maximum representation size of an object.

public static long
alignObjectSize(long size)

Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.

private static RamUsageEstimator.ClassCache
createCacheEntry(final Class<?> clazz)

Create a cached information about shallow size and reference fields for a given class.

public static EnumSet<RamUsageEstimator.JvmFeature>
getSupportedFeatures()

Return the set of supported JVM features that improve the estimation.

public static EnumSet<RamUsageEstimator.JvmFeature>
getUnsupportedFeatures()

Return the set of unsupported JVM features that improve the estimation.

public static String
humanReadableUnits(long bytes)

Returns size in human-readable units (GB, MB, KB or bytes).

public static String
humanReadableUnits(long bytes, DecimalFormat df)

Returns size in human-readable units (GB, MB, KB or bytes).

public static String
humanSizeOf(Object object)

Return a human-readable size of a given object.

public static boolean
isSupportedJVM()

Returns true, if the current JVM is fully supported by RamUsageEstimator.

private static long
measureSizeOf(ArrayList<Object>
Root objects.
stack
)

Non-recursive version of object descend.

public static long
shallowSizeOf(Object obj)

Estimates a "shallow" memory usage of the given object.

public static long
shallowSizeOfAll(Object... objects)

Same as shallowSizeOf(Object) but sums up all the arguments.

public static long
shallowSizeOfAll(Iterable<Object> objects)

Same as shallowSizeOf(Object) but sums up all the arguments.

private static long
shallowSizeOfArray(Object array)

Return shallow size of any array.

public static long
shallowSizeOfInstance(Class<?> clazz)

Returns the shallow instance size in bytes an instance of the given class would occupy.

public static long
sizeOf(byte[] arr)

Returns the size in bytes of the byte[] object.

public static long
sizeOf(boolean[] arr)

Returns the size in bytes of the boolean[] object.

public static long
sizeOf(char[] arr)

Returns the size in bytes of the char[] object.

public static long
sizeOf(short[] arr)

Returns the size in bytes of the short[] object.

public static long
sizeOf(int[] arr)

Returns the size in bytes of the int[] object.

public static long
sizeOf(float[] arr)

Returns the size in bytes of the float[] object.

public static long
sizeOf(long[] arr)

Returns the size in bytes of the long[] object.

public static long
sizeOf(double[] arr)

Returns the size in bytes of the double[] object.

public static long
sizeOf(Object obj)

Estimates the RAM usage by the given object.

public static long
sizeOfAll(Object... objects)

Same as sizeOf(Object) but sums up all the arguments.

public static long
sizeOfAll(Iterable<Object> objects)

Same as sizeOf(Object) but sums up all the arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

JVM_INFO_STRINGback to summary
public static final String JVM_INFO_STRING

JVM info string for debugging and reports.

NUM_BYTES_ARRAY_HEADERback to summary
public static final int NUM_BYTES_ARRAY_HEADER

Number of bytes to represent an array header (no content, but with alignments).

NUM_BYTES_BOOLEANback to summary
public static final int NUM_BYTES_BOOLEAN
NUM_BYTES_BYTEback to summary
public static final int NUM_BYTES_BYTE
NUM_BYTES_CHARback to summary
public static final int NUM_BYTES_CHAR
NUM_BYTES_DOUBLEback to summary
public static final int NUM_BYTES_DOUBLE
NUM_BYTES_FLOATback to summary
public static final int NUM_BYTES_FLOAT
NUM_BYTES_INTback to summary
public static final int NUM_BYTES_INT
NUM_BYTES_LONGback to summary
public static final int NUM_BYTES_LONG
NUM_BYTES_OBJECT_ALIGNMENTback to summary
public static final int NUM_BYTES_OBJECT_ALIGNMENT

A constant specifying the object alignment boundary inside the JVM. Objects will always take a full multiple of this constant, possibly wasting some space.

NUM_BYTES_OBJECT_HEADERback to summary
public static final int NUM_BYTES_OBJECT_HEADER

Number of bytes to represent an object header (no fields, no alignments).

NUM_BYTES_OBJECT_REFback to summary
public static final int NUM_BYTES_OBJECT_REF

Number of bytes this jvm uses to represent an object reference.

NUM_BYTES_SHORTback to summary
public static final int NUM_BYTES_SHORT
objectFieldOffsetMethodback to summary
private static final Method objectFieldOffsetMethod

A handle to sun.misc.Unsafe#fieldOffset(Field).

ONE_GBback to summary
public static final long ONE_GB

One gigabyte bytes.

ONE_KBback to summary
public static final long ONE_KB

One kilobyte bytes.

ONE_MBback to summary
public static final long ONE_MB

One megabyte bytes.

primitiveSizesback to summary
private static final Map<Class<?>, Integer> primitiveSizes

Sizes of primitive classes.

supportedFeaturesback to summary
private static final EnumSet<RamUsageEstimator.JvmFeature> supportedFeatures

All the supported "internal" JVM features detected at clinit.

theUnsafeback to summary
private static final Object theUnsafe

A handle to sun.misc.Unsafe.

Constructor Detail

RamUsageEstimatorback to summary
private RamUsageEstimator()

No instantiation.

Method Detail

adjustForFieldback to summary
private static long adjustForField(long sizeSoFar, final Field f)

This method returns the maximum representation size of an object. sizeSoFar is the object's size measured so far. f is the field being probed.

The returned offset will be the maximum of whatever was measured so far and f field's offset and representation size (unaligned).

alignObjectSizeback to summary
public static long alignObjectSize(long size)

Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.

createCacheEntryback to summary
private static RamUsageEstimator.ClassCache createCacheEntry(final Class<?> clazz)

Create a cached information about shallow size and reference fields for a given class.

getSupportedFeaturesback to summary
public static EnumSet<RamUsageEstimator.JvmFeature> getSupportedFeatures()

Return the set of supported JVM features that improve the estimation.

getUnsupportedFeaturesback to summary
public static EnumSet<RamUsageEstimator.JvmFeature> getUnsupportedFeatures()

Return the set of unsupported JVM features that improve the estimation.

humanReadableUnitsback to summary
public static String humanReadableUnits(long bytes)

Returns size in human-readable units (GB, MB, KB or bytes).

humanReadableUnitsback to summary
public static String humanReadableUnits(long bytes, DecimalFormat df)

Returns size in human-readable units (GB, MB, KB or bytes).

humanSizeOfback to summary
public static String humanSizeOf(Object object)

Return a human-readable size of a given object.

See Also
sizeOf(Object), humanReadableUnits(long)
isSupportedJVMback to summary
public static boolean isSupportedJVM()

Returns true, if the current JVM is fully supported by RamUsageEstimator. If this method returns false you are maybe using a 3rd party Java VM that is not supporting Oracle/Sun private APIs. The memory estimates can be imprecise then (no way of detecting compressed references, alignments, etc.). Lucene still tries to use sensible defaults.

measureSizeOfback to summary
private static long measureSizeOf(ArrayList<Object> stack)

Non-recursive version of object descend. This consumes more memory than recursive in-depth traversal but prevents stack overflows on long chains of objects or complex graphs (a max. recursion depth on my machine was ~5000 objects linked in a chain so not too much).

Parameters
stack:ArrayList<Object>

Root objects.

shallowSizeOfback to summary
public static long shallowSizeOf(Object obj)

Estimates a "shallow" memory usage of the given object. For arrays, this will be the memory taken by array storage (no subreferences will be followed). For objects, this will be the memory taken by the fields. JVM object alignments are also applied.

shallowSizeOfAllback to summary
public static long shallowSizeOfAll(Object... objects)

Same as shallowSizeOf(Object) but sums up all the arguments. Not an overload to prevent accidental parameter conflicts with shallowSizeOf(Object).

shallowSizeOfAllback to summary
public static long shallowSizeOfAll(Iterable<Object> objects)

Same as shallowSizeOf(Object) but sums up all the arguments. Duplicate objects are not resolved and will be counted twice. Not an overload to prevent accidental parameter conflicts with shallowSizeOf(Object).

shallowSizeOfArrayback to summary
private static long shallowSizeOfArray(Object array)

Return shallow size of any array.

shallowSizeOfInstanceback to summary
public static long shallowSizeOfInstance(Class<?> clazz)

Returns the shallow instance size in bytes an instance of the given class would occupy. This works with all conventional classes and primitive types, but not with arrays (the size then depends on the number of elements and varies from object to object).

Exceptions
IllegalArgumentException:
if clazz is an array class.
See Also
shallowSizeOf(Object)
sizeOfback to summary
public static long sizeOf(byte[] arr)

Returns the size in bytes of the byte[] object.

sizeOfback to summary
public static long sizeOf(boolean[] arr)

Returns the size in bytes of the boolean[] object.

sizeOfback to summary
public static long sizeOf(char[] arr)

Returns the size in bytes of the char[] object.

sizeOfback to summary
public static long sizeOf(short[] arr)

Returns the size in bytes of the short[] object.

sizeOfback to summary
public static long sizeOf(int[] arr)

Returns the size in bytes of the int[] object.

sizeOfback to summary
public static long sizeOf(float[] arr)

Returns the size in bytes of the float[] object.

sizeOfback to summary
public static long sizeOf(long[] arr)

Returns the size in bytes of the long[] object.

sizeOfback to summary
public static long sizeOf(double[] arr)

Returns the size in bytes of the double[] object.

sizeOfback to summary
public static long sizeOf(Object obj)

Estimates the RAM usage by the given object. It will walk the object tree and sum up all referenced objects.

Resource Usage: This method internally uses a set of every object seen during traversals so it does allocate memory (it isn't side-effect free). After the method exits, this memory should be GCed.

sizeOfAllback to summary
public static long sizeOfAll(Object... objects)

Same as sizeOf(Object) but sums up all the arguments. Not an overload to prevent accidental parameter conflicts with sizeOf(Object).

sizeOfAllback to summary
public static long sizeOfAll(Iterable<Object> objects)

Same as sizeOf(Object) but sums up all the arguments. Not an overload to prevent accidental parameter conflicts with sizeOf(Object).

com.carrotsearch.sizeof back to summary

private final Class RamUsageEstimator.ClassCache

extends Object
Class Inheritance

Cached information about a given class.

Field Summary

Modifier and TypeField and Description
public final long
public final Field[]

Constructor Summary

AccessConstructor and Description
public
ClassCache(long alignedShallowInstanceSize, Field[] referenceFields)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alignedShallowInstanceSizeback to summary
public final long alignedShallowInstanceSize
referenceFieldsback to summary
public final Field[] referenceFields

Constructor Detail

ClassCacheback to summary
public ClassCache(long alignedShallowInstanceSize, Field[] referenceFields)
com.carrotsearch.sizeof back to summary

private final Class RamUsageEstimator.DummyOneFieldObject

extends Object
Class Inheritance
  • java.lang.Object
  • com.carrotsearch.sizeof.RamUsageEstimator.DummyOneFieldObject
Annotations
@SuppressWarnings:unused

Field Summary

Modifier and TypeField and Description
public byte

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

baseback to summary
public byte base

Constructor Detail

DummyOneFieldObjectback to summary
private DummyOneFieldObject()
com.carrotsearch.sizeof back to summary

private final Class RamUsageEstimator.DummyTwoLongObject

extends Object
Class Inheritance
Annotations
@SuppressWarnings:unused

Field Summary

Modifier and TypeField and Description
public long
public long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dummy1back to summary
public long dummy1
dummy2back to summary
public long dummy2

Constructor Detail

DummyTwoLongObjectback to summary
private DummyTwoLongObject()
com.carrotsearch.sizeof back to summary

public final Enum RamUsageEstimator.JvmFeature

extends Enum<RamUsageEstimator.JvmFeature>
Class Inheritance

JVM diagnostic features.

Field Summary

Modifier and TypeField and Description
public static final RamUsageEstimator.JvmFeature
public final String
public static final RamUsageEstimator.JvmFeature
public static final RamUsageEstimator.JvmFeature
public static final RamUsageEstimator.JvmFeature

Constructor Summary

AccessConstructor and Description
private
JvmFeature(String description)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static RamUsageEstimator.JvmFeature
public static RamUsageEstimator.JvmFeature[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

ARRAY_HEADER_SIZEback to summary
public static final RamUsageEstimator.JvmFeature ARRAY_HEADER_SIZE
descriptionback to summary
public final String description
FIELD_OFFSETSback to summary
public static final RamUsageEstimator.JvmFeature FIELD_OFFSETS
OBJECT_ALIGNMENTback to summary
public static final RamUsageEstimator.JvmFeature OBJECT_ALIGNMENT
OBJECT_REFERENCE_SIZEback to summary
public static final RamUsageEstimator.JvmFeature OBJECT_REFERENCE_SIZE

Constructor Detail

JvmFeatureback to summary
private JvmFeature(String description)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static RamUsageEstimator.JvmFeature valueOf(String name)
valuesback to summary
public static RamUsageEstimator.JvmFeature[] values()