Class RamUsageEstimator
- java.lang.Object
-
- com.carrotsearch.randomizedtesting.rules.RamUsageEstimator
-
final class RamUsageEstimator extends java.lang.ObjectEstimates the size (memory representation) of Java objects.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classRamUsageEstimator.ClassCacheCached information about a given class.private static classRamUsageEstimator.DummyOneFieldObjectprivate static classRamUsageEstimator.DummyTwoLongObjectstatic classRamUsageEstimator.JvmFeatureJVM diagnostic features.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringJVM_INFO_STRINGJVM info string for debugging and reports.static intNUM_BYTES_ARRAY_HEADERNumber of bytes to represent an array header (no content, but with alignments).static intNUM_BYTES_BOOLEANstatic intNUM_BYTES_BYTEstatic intNUM_BYTES_CHARstatic intNUM_BYTES_DOUBLEstatic intNUM_BYTES_FLOATstatic intNUM_BYTES_INTstatic intNUM_BYTES_LONGstatic intNUM_BYTES_OBJECT_ALIGNMENTA constant specifying the object alignment boundary inside the JVM.static intNUM_BYTES_OBJECT_HEADERNumber of bytes to represent an object header (no fields, no alignments).static intNUM_BYTES_OBJECT_REFNumber of bytes this jvm uses to represent an object reference.static intNUM_BYTES_SHORTprivate static java.lang.reflect.MethodobjectFieldOffsetMethodA handle tosun.misc.Unsafe#fieldOffset(Field).static longONE_GBOne gigabyte bytes.static longONE_KBOne kilobyte bytes.static longONE_MBOne megabyte bytes.private static java.util.Map<java.lang.Class<?>,java.lang.Integer>primitiveSizesSizes of primitive classes.private static java.util.EnumSet<RamUsageEstimator.JvmFeature>supportedFeaturesAll the supported "internal" JVM features detected at clinit.private static java.lang.ObjecttheUnsafeA handle tosun.misc.Unsafe.
-
Constructor Summary
Constructors Modifier Constructor Description privateRamUsageEstimator()No instantiation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static longadjustForField(long sizeSoFar, java.lang.reflect.Field f)This method returns the maximum representation size of an object.static longalignObjectSize(long size)Aligns an object size to be the next multiple ofNUM_BYTES_OBJECT_ALIGNMENT.private static RamUsageEstimator.ClassCachecreateCacheEntry(java.lang.Class<?> clazz)Create a cached information about shallow size and reference fields for a given class.static java.util.EnumSet<RamUsageEstimator.JvmFeature>getSupportedFeatures()Return the set of supported JVM features that improve the estimation.static java.util.EnumSet<RamUsageEstimator.JvmFeature>getUnsupportedFeatures()Return the set of unsupported JVM features that improve the estimation.static java.lang.StringhumanReadableUnits(long bytes)Returnssizein human-readable units (GB, MB, KB or bytes).static java.lang.StringhumanReadableUnits(long bytes, java.text.DecimalFormat df)Returnssizein human-readable units (GB, MB, KB or bytes).static java.lang.StringhumanSizeOf(java.lang.Object object)Return a human-readable size of a given object.static booleanisSupportedJVM()Returns true, if the current JVM is fully supported byRamUsageEstimator.private static longmeasureSizeOf(java.util.ArrayList<java.lang.Object> stack)Non-recursive version of object descend.static longshallowSizeOf(java.lang.Object obj)Estimates a "shallow" memory usage of the given object.static longshallowSizeOfAll(java.lang.Iterable<java.lang.Object> objects)Same asshallowSizeOf(Object)but sums up all the arguments.static longshallowSizeOfAll(java.lang.Object... objects)Same asshallowSizeOf(Object)but sums up all the arguments.private static longshallowSizeOfArray(java.lang.Object array)Return shallow size of anyarray.static longshallowSizeOfInstance(java.lang.Class<?> clazz)Returns the shallow instance size in bytes an instance of the given class would occupy.static longsizeOf(boolean[] arr)Returns the size in bytes of the boolean[] object.static longsizeOf(byte[] arr)Returns the size in bytes of the byte[] object.static longsizeOf(char[] arr)Returns the size in bytes of the char[] object.static longsizeOf(double[] arr)Returns the size in bytes of the double[] object.static longsizeOf(float[] arr)Returns the size in bytes of the float[] object.static longsizeOf(int[] arr)Returns the size in bytes of the int[] object.static longsizeOf(long[] arr)Returns the size in bytes of the long[] object.static longsizeOf(short[] arr)Returns the size in bytes of the short[] object.static longsizeOf(java.lang.Object obj)Estimates the RAM usage by the given object.static longsizeOfAll(java.lang.Iterable<java.lang.Object> objects)Same assizeOf(Object)but sums up all the arguments.static longsizeOfAll(java.lang.Object... objects)Same assizeOf(Object)but sums up all the arguments.
-
-
-
Field Detail
-
JVM_INFO_STRING
public static final java.lang.String JVM_INFO_STRING
JVM info string for debugging and reports.
-
ONE_KB
public static final long ONE_KB
One kilobyte bytes.- See Also:
- Constant Field Values
-
ONE_MB
public static final long ONE_MB
One megabyte bytes.- See Also:
- Constant Field Values
-
ONE_GB
public static final long ONE_GB
One gigabyte bytes.- See Also:
- Constant Field Values
-
NUM_BYTES_BOOLEAN
public static final int NUM_BYTES_BOOLEAN
- See Also:
- Constant Field Values
-
NUM_BYTES_BYTE
public static final int NUM_BYTES_BYTE
- See Also:
- Constant Field Values
-
NUM_BYTES_CHAR
public static final int NUM_BYTES_CHAR
- See Also:
- Constant Field Values
-
NUM_BYTES_SHORT
public static final int NUM_BYTES_SHORT
- See Also:
- Constant Field Values
-
NUM_BYTES_INT
public static final int NUM_BYTES_INT
- See Also:
- Constant Field Values
-
NUM_BYTES_FLOAT
public static final int NUM_BYTES_FLOAT
- See Also:
- Constant Field Values
-
NUM_BYTES_LONG
public static final int NUM_BYTES_LONG
- See Also:
- Constant Field Values
-
NUM_BYTES_DOUBLE
public static final int NUM_BYTES_DOUBLE
- See Also:
- Constant Field Values
-
NUM_BYTES_OBJECT_REF
public static final int NUM_BYTES_OBJECT_REF
Number of bytes this jvm uses to represent an object reference.
-
NUM_BYTES_OBJECT_HEADER
public static final int NUM_BYTES_OBJECT_HEADER
Number of bytes to represent an object header (no fields, no alignments).
-
NUM_BYTES_ARRAY_HEADER
public static final int NUM_BYTES_ARRAY_HEADER
Number of bytes to represent an array header (no content, but with alignments).
-
NUM_BYTES_OBJECT_ALIGNMENT
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.
-
primitiveSizes
private static final java.util.Map<java.lang.Class<?>,java.lang.Integer> primitiveSizes
Sizes of primitive classes.
-
theUnsafe
private static final java.lang.Object theUnsafe
A handle tosun.misc.Unsafe.
-
objectFieldOffsetMethod
private static final java.lang.reflect.Method objectFieldOffsetMethod
A handle tosun.misc.Unsafe#fieldOffset(Field).
-
supportedFeatures
private static final java.util.EnumSet<RamUsageEstimator.JvmFeature> supportedFeatures
All the supported "internal" JVM features detected at clinit.
-
-
Method Detail
-
isSupportedJVM
public static boolean isSupportedJVM()
Returns true, if the current JVM is fully supported byRamUsageEstimator. If this method returnsfalseyou 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
public static long sizeOf(java.lang.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.
-
sizeOfAll
public static long sizeOfAll(java.lang.Object... objects)
Same assizeOf(Object)but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withsizeOf(Object).
-
sizeOfAll
public static long sizeOfAll(java.lang.Iterable<java.lang.Object> objects)
Same assizeOf(Object)but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withsizeOf(Object).
-
shallowSizeOf
public static long shallowSizeOf(java.lang.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.
-
shallowSizeOfAll
public static long shallowSizeOfAll(java.lang.Object... objects)
Same asshallowSizeOf(Object)but sums up all the arguments. Not an overload to prevent accidental parameter conflicts withshallowSizeOf(Object).
-
shallowSizeOfAll
public static long shallowSizeOfAll(java.lang.Iterable<java.lang.Object> objects)
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
public static long shallowSizeOfInstance(java.lang.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).- Throws:
java.lang.IllegalArgumentException- ifclazzis an array class.- See Also:
shallowSizeOf(Object)
-
getUnsupportedFeatures
public static java.util.EnumSet<RamUsageEstimator.JvmFeature> getUnsupportedFeatures()
Return the set of unsupported JVM features that improve the estimation.
-
getSupportedFeatures
public static java.util.EnumSet<RamUsageEstimator.JvmFeature> getSupportedFeatures()
Return the set of supported JVM features that improve the estimation.
-
shallowSizeOfArray
private static long shallowSizeOfArray(java.lang.Object array)
Return shallow size of anyarray.
-
measureSizeOf
private static long measureSizeOf(java.util.ArrayList<java.lang.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- Root objects.
-
createCacheEntry
private static RamUsageEstimator.ClassCache createCacheEntry(java.lang.Class<?> clazz)
Create a cached information about shallow size and reference fields for a given class.
-
adjustForField
private static long adjustForField(long sizeSoFar, java.lang.reflect.Field f)This method returns the maximum representation size of an object.sizeSoFaris the object's size measured so far.fis the field being probed.The returned offset will be the maximum of whatever was measured so far and
ffield's offset and representation size (unaligned).
-
humanReadableUnits
public static java.lang.String humanReadableUnits(long bytes)
Returnssizein human-readable units (GB, MB, KB or bytes).
-
humanReadableUnits
public static java.lang.String humanReadableUnits(long bytes, java.text.DecimalFormat df)Returnssizein human-readable units (GB, MB, KB or bytes).
-
humanSizeOf
public static java.lang.String humanSizeOf(java.lang.Object object)
Return a human-readable size of a given object.- See Also:
sizeOf(Object),humanReadableUnits(long)
-
-