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 long
HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a Hashtable / HashMap entry.static long
LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a LinkedHashMap entry.static int
MAX_DEPTH
Recurse only into immediate descendants.static int
NUM_BYTES_ARRAY_HEADER
Number of bytes to represent an array header (no content, but with alignments).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 long
ONE_GB
One gigabyte bytes.static long
ONE_KB
One kilobyte bytes.static long
ONE_MB
One megabyte bytes.static Map<Class<?>,Integer>
primitiveSizes
Sizes of primitive classes.static int
QUERY_DEFAULT_RAM_BYTES_USED
Approximate memory usage that we assign to all unknown queries - this maps roughly to a BooleanQuery with a couple term clauses.static int
UNKNOWN_DEFAULT_RAM_BYTES_USED
Approximate memory usage that we assign to all unknown objects - this maps roughly to a few primitive fields and a couple short String-s.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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
.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(boolean[] arr)
Returns the size in bytes of the boolean[] object.static long
shallowSizeOf(byte[] arr)
Returns the size in bytes of the byte[] object.static long
shallowSizeOf(char[] arr)
Returns the size in bytes of the char[] object.static long
shallowSizeOf(double[] arr)
Returns the size in bytes of the double[] object.static long
shallowSizeOf(float[] arr)
Returns the size in bytes of the float[] object.static long
shallowSizeOf(int[] arr)
Returns the size in bytes of the int[] object.static long
shallowSizeOf(long[] arr)
Returns the size in bytes of the long[] object.static long
shallowSizeOf(short[] arr)
Returns the size in bytes of the short[] object.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(Integer ignored)
Return the shallow size of the providedInteger
object.static long
sizeOf(Long ignored)
Return the shallow size of the providedLong
object.static long
sizeOf(String s)
Returns the size in bytes of the String object.static long
sizeOf(String[] arr)
Returns the size in bytes of the String[] object.static long
sizeOf(Query q)
Returns the size in bytes of a Query object.static long
sizeOf(Query q, long defSize)
Returns the size in bytes of a Query object.static long
sizeOf(Accountable accountable)
Returns the size in bytes of theAccountable
object, using itsAccountable.ramBytesUsed()
method.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
.static long
sizeOfCollection(Collection<?> collection)
Returns the size in bytes of a Collection object, including sizes of its values, supplyingUNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known.static long
sizeOfCollection(Collection<?> collection, long defSize)
Returns the size in bytes of a Collection object, including sizes of its values, supplying default object size when object type is not well known.static long
sizeOfMap(Map<?,?> map)
Returns the size in bytes of a Map object, including sizes of its keys and values, supplyingUNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known.static long
sizeOfMap(Map<?,?> map, long defSize)
Returns the size in bytes of a Map object, including sizes of its keys and values, supplying default object size when object type is not well known.static long
sizeOfObject(Object o)
Best effort attempt to estimate the size in bytes of an undetermined object.static long
sizeOfObject(Object o, long defSize)
Best effort attempt to estimate the size in bytes of an undetermined object.
-
-
-
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
-
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.
-
QUERY_DEFAULT_RAM_BYTES_USED
public static final int QUERY_DEFAULT_RAM_BYTES_USED
Approximate memory usage that we assign to all unknown queries - this maps roughly to a BooleanQuery with a couple term clauses.- See Also:
- Constant Field Values
-
UNKNOWN_DEFAULT_RAM_BYTES_USED
public static final int UNKNOWN_DEFAULT_RAM_BYTES_USED
Approximate memory usage that we assign to all unknown objects - this maps roughly to a few primitive fields and a couple short String-s.- See Also:
- Constant Field Values
-
primitiveSizes
public static final Map<Class<?>,Integer> primitiveSizes
Sizes of primitive classes.
-
HASHTABLE_RAM_BYTES_PER_ENTRY
public static final long HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a Hashtable / HashMap entry.
-
LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY
public static final long LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a LinkedHashMap entry.
-
MAX_DEPTH
public static final int MAX_DEPTH
Recurse only into immediate descendants.- See Also:
- Constant Field Values
-
-
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(Integer ignored)
Return the shallow size of the providedInteger
object. Ignores the possibility that this object is part of the VM IntegerCache
-
sizeOf
public static long sizeOf(Long ignored)
Return the shallow size of the providedLong
object. Ignores the possibility that this object is part of the VM LongCache
-
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(String[] arr)
Returns the size in bytes of the String[] object.
-
sizeOfMap
public static long sizeOfMap(Map<?,?> map)
Returns the size in bytes of a Map object, including sizes of its keys and values, supplyingUNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known. This method recurses up toMAX_DEPTH
.
-
sizeOfMap
public static long sizeOfMap(Map<?,?> map, long defSize)
Returns the size in bytes of a Map object, including sizes of its keys and values, supplying default object size when object type is not well known. This method recurses up toMAX_DEPTH
.
-
sizeOfCollection
public static long sizeOfCollection(Collection<?> collection)
Returns the size in bytes of a Collection object, including sizes of its values, supplyingUNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known. This method recurses up toMAX_DEPTH
.
-
sizeOfCollection
public static long sizeOfCollection(Collection<?> collection, long defSize)
Returns the size in bytes of a Collection object, including sizes of its values, supplying default object size when object type is not well known. This method recurses up toMAX_DEPTH
.
-
sizeOf
public static long sizeOf(Query q)
Returns the size in bytes of a Query object. Unknown query types will be estimated asQUERY_DEFAULT_RAM_BYTES_USED
.
-
sizeOf
public static long sizeOf(Query q, long defSize)
Returns the size in bytes of a Query object. Unknown query types will be estimated usingshallowSizeOf(Object)
, or using the supplieddefSize
parameter if its value is greater than 0.
-
sizeOfObject
public static long sizeOfObject(Object o)
Best effort attempt to estimate the size in bytes of an undetermined object. Known types will be estimated according to their formulas, and all other object sizes will be estimated asUNKNOWN_DEFAULT_RAM_BYTES_USED
.
-
sizeOfObject
public static long sizeOfObject(Object o, long defSize)
Best effort attempt to estimate the size in bytes of an undetermined object. Known types will be estimated according to their formulas, and all other object sizes will be estimated usingshallowSizeOf(Object)
, or using the supplieddefSize
parameter if its value is greater than 0.
-
sizeOf
public static long sizeOf(Accountable accountable)
Returns the size in bytes of theAccountable
object, using itsAccountable.ramBytesUsed()
method.
-
sizeOf
public static long sizeOf(String s)
Returns the size in bytes of the String object.
-
shallowSizeOf
public static long shallowSizeOf(byte[] arr)
Returns the size in bytes of the byte[] object.
-
shallowSizeOf
public static long shallowSizeOf(boolean[] arr)
Returns the size in bytes of the boolean[] object.
-
shallowSizeOf
public static long shallowSizeOf(char[] arr)
Returns the size in bytes of the char[] object.
-
shallowSizeOf
public static long shallowSizeOf(short[] arr)
Returns the size in bytes of the short[] object.
-
shallowSizeOf
public static long shallowSizeOf(int[] arr)
Returns the size in bytes of the int[] object.
-
shallowSizeOf
public static long shallowSizeOf(float[] arr)
Returns the size in bytes of the float[] object.
-
shallowSizeOf
public static long shallowSizeOf(long[] arr)
Returns the size in bytes of the long[] object.
-
shallowSizeOf
public static long shallowSizeOf(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)
-
adjustForField
public static long adjustForField(long sizeSoFar, 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).
-
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
.
-
-