Class RamUsageEstimator
java.lang.Object
com.carrotsearch.randomizedtesting.rules.RamUsageEstimator
Estimates the size (memory representation) of Java objects.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Cached information about a given class.private static final class
private static final class
static enum
JVM diagnostic features. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
JVM info string for debugging and reports.static final int
Number of bytes to represent an array header (no content, but with alignments).static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
A constant specifying the object alignment boundary inside the JVM.static final int
Number of bytes to represent an object header (no fields, no alignments).static final int
Number of bytes this jvm uses to represent an object reference.static final int
private static final Method
A handle tosun.misc.Unsafe#fieldOffset(Field)
.static final long
One gigabyte bytes.static final long
One kilobyte bytes.static final long
One megabyte bytes.Sizes of primitive classes.private static final EnumSet
<RamUsageEstimator.JvmFeature> All the supported "internal" JVM features detected at clinit.private static final Object
A handle tosun.misc.Unsafe
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static long
adjustForField
(long sizeSoFar, Field f) This method returns the maximum representation size of an object.static long
alignObjectSize
(long size) Aligns an object size to be the next multiple ofNUM_BYTES_OBJECT_ALIGNMENT
.private static RamUsageEstimator.ClassCache
createCacheEntry
(Class<?> clazz) Create a cached information about shallow size and reference fields for a given class.static EnumSet
<RamUsageEstimator.JvmFeature> Return the set of supported JVM features that improve the estimation.static EnumSet
<RamUsageEstimator.JvmFeature> Return the set of unsupported JVM features that improve the estimation.static String
humanReadableUnits
(long bytes) Returnssize
in human-readable units (GB, MB, KB or bytes).static String
humanReadableUnits
(long bytes, DecimalFormat df) Returnssize
in human-readable units (GB, MB, KB or bytes).static String
humanSizeOf
(Object object) Return a human-readable size of a given object.static boolean
Returns true, if the current JVM is fully supported byRamUsageEstimator
.private static long
measureSizeOf
(ArrayList<Object> stack) Non-recursive version of object descend.static long
shallowSizeOf
(Object obj) Estimates a "shallow" memory usage of the given object.static long
shallowSizeOfAll
(Iterable<Object> objects) Same asshallowSizeOf(Object)
but sums up all the arguments.static long
shallowSizeOfAll
(Object... objects) Same asshallowSizeOf(Object)
but sums up all the arguments.private static long
shallowSizeOfArray
(Object array) Return shallow size of anyarray
.static long
shallowSizeOfInstance
(Class<?> clazz) Returns the shallow instance size in bytes an instance of the given class would occupy.static long
sizeOf
(boolean[] arr) Returns the size in bytes of the boolean[] object.static long
sizeOf
(byte[] arr) Returns the size in bytes of the byte[] object.static long
sizeOf
(char[] arr) Returns the size in bytes of the char[] object.static long
sizeOf
(double[] arr) Returns the size in bytes of the double[] object.static long
sizeOf
(float[] arr) Returns the size in bytes of the float[] object.static long
sizeOf
(int[] arr) Returns the size in bytes of the int[] object.static long
sizeOf
(long[] arr) Returns the size in bytes of the long[] object.static long
sizeOf
(short[] arr) Returns the size in bytes of the short[] object.static long
Estimates the RAM usage by the given object.static long
Same assizeOf(Object)
but sums up all the arguments.static long
Same assizeOf(Object)
but sums up all the arguments.
-
Field Details
-
JVM_INFO_STRING
JVM info string for debugging and reports. -
ONE_KB
public static final long ONE_KBOne kilobyte bytes.- See Also:
-
ONE_MB
public static final long ONE_MBOne megabyte bytes.- See Also:
-
ONE_GB
public static final long ONE_GBOne gigabyte bytes.- See Also:
-
NUM_BYTES_BOOLEAN
public static final int NUM_BYTES_BOOLEAN- See Also:
-
NUM_BYTES_BYTE
public static final int NUM_BYTES_BYTE- See Also:
-
NUM_BYTES_CHAR
public static final int NUM_BYTES_CHAR- See Also:
-
NUM_BYTES_SHORT
public static final int NUM_BYTES_SHORT- See Also:
-
NUM_BYTES_INT
public static final int NUM_BYTES_INT- See Also:
-
NUM_BYTES_FLOAT
public static final int NUM_BYTES_FLOAT- See Also:
-
NUM_BYTES_LONG
public static final int NUM_BYTES_LONG- See Also:
-
NUM_BYTES_DOUBLE
public static final int NUM_BYTES_DOUBLE- See Also:
-
NUM_BYTES_OBJECT_REF
public static final int NUM_BYTES_OBJECT_REFNumber of bytes this jvm uses to represent an object reference. -
NUM_BYTES_OBJECT_HEADER
public static final int NUM_BYTES_OBJECT_HEADERNumber of bytes to represent an object header (no fields, no alignments). -
NUM_BYTES_ARRAY_HEADER
public static final int NUM_BYTES_ARRAY_HEADERNumber of bytes to represent an array header (no content, but with alignments). -
NUM_BYTES_OBJECT_ALIGNMENT
public static final int NUM_BYTES_OBJECT_ALIGNMENTA constant specifying the object alignment boundary inside the JVM. Objects will always take a full multiple of this constant, possibly wasting some space. -
primitiveSizes
Sizes of primitive classes. -
theUnsafe
A handle tosun.misc.Unsafe
. -
objectFieldOffsetMethod
A handle tosun.misc.Unsafe#fieldOffset(Field)
. -
supportedFeatures
All the supported "internal" JVM features detected at clinit.
-
-
Constructor Details
-
RamUsageEstimator
private RamUsageEstimator()No instantiation.
-
-
Method Details
-
isSupportedJVM
public static boolean isSupportedJVM()Returns true, if the current JVM is fully supported byRamUsageEstimator
. If this method returnsfalse
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. -
alignObjectSize
public static long alignObjectSize(long size) Aligns an object size to be the next multiple ofNUM_BYTES_OBJECT_ALIGNMENT
. -
sizeOf
public static long sizeOf(byte[] arr) Returns the size in bytes of the byte[] object. -
sizeOf
public static long sizeOf(boolean[] arr) Returns the size in bytes of the boolean[] object. -
sizeOf
public static long sizeOf(char[] arr) Returns the size in bytes of the char[] object. -
sizeOf
public static long sizeOf(short[] arr) Returns the size in bytes of the short[] object. -
sizeOf
public static long sizeOf(int[] arr) Returns the size in bytes of the int[] object. -
sizeOf
public static long sizeOf(float[] arr) Returns the size in bytes of the float[] object. -
sizeOf
public static long sizeOf(long[] arr) Returns the size in bytes of the long[] object. -
sizeOf
public static long sizeOf(double[] arr) Returns the size in bytes of the double[] object. -
sizeOf
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.
-
sizeOfAll
Same assizeOf(Object)
but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withsizeOf(Object)
. -
sizeOfAll
Same assizeOf(Object)
but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withsizeOf(Object)
. -
shallowSizeOf
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. -
shallowSizeOfAll
Same asshallowSizeOf(Object)
but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withshallowSizeOf(Object)
. -
shallowSizeOfAll
Same asshallowSizeOf(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 withshallowSizeOf(Object)
. -
shallowSizeOfInstance
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).- Throws:
IllegalArgumentException
- ifclazz
is an array class.- See Also:
-
getUnsupportedFeatures
Return the set of unsupported JVM features that improve the estimation. -
getSupportedFeatures
Return the set of supported JVM features that improve the estimation. -
shallowSizeOfArray
Return shallow size of anyarray
. -
measureSizeOf
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
- Root objects.
-
createCacheEntry
Create a cached information about shallow size and reference fields for a given class. -
adjustForField
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). -
humanReadableUnits
Returnssize
in human-readable units (GB, MB, KB or bytes). -
humanReadableUnits
Returnssize
in human-readable units (GB, MB, KB or bytes). -
humanSizeOf
Return a human-readable size of a given object.- See Also:
-