Package org.apache.lucene.util
Class RamUsageEstimator
- java.lang.Object
-
- org.apache.lucene.util.RamUsageEstimator
-
public final class RamUsageEstimator extends Object
Estimates the size (memory representation) of Java objects.This class uses assumptions that were discovered for the Hotspot virtual machine. If you use a non-OpenJDK/Oracle-based JVM, the measurements may be slightly wrong.
- See Also:
shallowSizeOf(Object)
,shallowSizeOfInstance(Class)
- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
COMPRESSED_REFS_ENABLED
True, iff compressed references (oops) are enabled by this JVMstatic int
NUM_BYTES_ARRAY_HEADER
Number of bytes to represent an array header (no content, but with alignments).static int
NUM_BYTES_BOOLEAN
Deprecated.use1
instead.static int
NUM_BYTES_BYTE
Deprecated.use1
instead.static int
NUM_BYTES_CHAR
Deprecated.useCharacter.BYTES
instead.static int
NUM_BYTES_DOUBLE
Deprecated.useDouble.BYTES
instead.static int
NUM_BYTES_FLOAT
Deprecated.useFloat.BYTES
instead.static int
NUM_BYTES_INT
Deprecated.useInteger.BYTES
instead.static int
NUM_BYTES_LONG
Deprecated.useLong.BYTES
instead.static int
NUM_BYTES_OBJECT_ALIGNMENT
A constant specifying the object alignment boundary inside the JVM.static int
NUM_BYTES_OBJECT_HEADER
Number of bytes to represent an object header (no fields, no alignments).static int
NUM_BYTES_OBJECT_REF
Number of bytes this JVM uses to represent an object reference.static int
NUM_BYTES_SHORT
Deprecated.useShort.BYTES
instead.static long
ONE_GB
One gigabyte bytes.static long
ONE_KB
One kilobyte bytes.static long
ONE_MB
One megabyte bytes.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
alignObjectSize(long size)
Aligns an object size to be the next multiple ofNUM_BYTES_OBJECT_ALIGNMENT
.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 long
shallowSizeOf(Object obj)
Estimates a "shallow" memory usage of the given object.static long
shallowSizeOf(Object[] arr)
Returns the shallow size in bytes of the Object[] object.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
sizeOf(Long value)
Return the size of the providedLong
object, returning 0 if it is cached by the JVM and its shallow size otherwise.static long
sizeOf(Accountable[] accountables)
Return the size of the provided array ofAccountable
s by summing up the shallow size of the array and thememory usage
reported by eachAccountable
.
-
-
-
Field Detail
-
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
@Deprecated public static final int NUM_BYTES_BOOLEAN
Deprecated.use1
instead.Number of bytes used to represent aboolean
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_BYTE
@Deprecated public static final int NUM_BYTES_BYTE
Deprecated.use1
instead.Number of bytes used to represent abyte
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_CHAR
@Deprecated public static final int NUM_BYTES_CHAR
Deprecated.useCharacter.BYTES
instead.Number of bytes used to represent achar
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_SHORT
@Deprecated public static final int NUM_BYTES_SHORT
Deprecated.useShort.BYTES
instead.Number of bytes used to represent ashort
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_INT
@Deprecated public static final int NUM_BYTES_INT
Deprecated.useInteger.BYTES
instead.Number of bytes used to represent anint
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_FLOAT
@Deprecated public static final int NUM_BYTES_FLOAT
Deprecated.useFloat.BYTES
instead.Number of bytes used to represent afloat
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_LONG
@Deprecated public static final int NUM_BYTES_LONG
Deprecated.useLong.BYTES
instead.Number of bytes used to represent along
in binary form- See Also:
- Constant Field Values
-
NUM_BYTES_DOUBLE
@Deprecated public static final int NUM_BYTES_DOUBLE
Deprecated.useDouble.BYTES
instead.Number of bytes used to represent adouble
in binary form- See Also:
- Constant Field Values
-
COMPRESSED_REFS_ENABLED
public static final boolean COMPRESSED_REFS_ENABLED
True, iff compressed references (oops) are enabled by this JVM
-
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.
-
-
Method Detail
-
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(Long value)
Return the size of the providedLong
object, returning 0 if it is cached by the JVM and its shallow size otherwise.
-
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.
-
shallowSizeOf
public static long shallowSizeOf(Object[] arr)
Returns the shallow size in bytes of the Object[] object.
-
shallowSizeOf
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.
-
shallowSizeOfInstance
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).- Throws:
IllegalArgumentException
- ifclazz
is an array class.- See Also:
shallowSizeOf(Object)
-
humanReadableUnits
public static String humanReadableUnits(long bytes)
Returnssize
in human-readable units (GB, MB, KB or bytes).
-
humanReadableUnits
public static String humanReadableUnits(long bytes, DecimalFormat df)
Returnssize
in human-readable units (GB, MB, KB or bytes).
-
sizeOf
public static long sizeOf(Accountable[] accountables)
Return the size of the provided array ofAccountable
s by summing up the shallow size of the array and thememory usage
reported by eachAccountable
.
-
-