public final class RamUsageEstimator
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
private static class |
RamUsageEstimator.RamUsageQueryVisitor |
Modifier and Type | Field and Description |
---|---|
static boolean |
COMPRESSED_REFS_ENABLED
True, iff compressed references (oops) are enabled by this JVM
|
static long |
HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a Hashtable / HashMap entry.
|
(package private) static java.lang.String |
HOTSPOT_BEAN_CLASS |
(package private) static boolean |
JVM_IS_HOTSPOT_64BIT
For testing only
|
static long |
LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY
Approximate memory usage that we assign to a LinkedHashMap entry.
|
(package private) static long |
LONG_CACHE_MAX_VALUE
JVMs typically cache small longs.
|
(package private) static long |
LONG_CACHE_MIN_VALUE
JVMs typically cache small longs.
|
(package private) static int |
LONG_SIZE |
(package private) static java.lang.String |
MANAGEMENT_FACTORY_CLASS |
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 java.util.Map<java.lang.Class<?>,java.lang.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.
|
(package private) static int |
STRING_SIZE |
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.
|
Modifier | Constructor and Description |
---|---|
private |
RamUsageEstimator()
No instantiation.
|
Modifier and Type | Method and Description |
---|---|
(package private) static long |
adjustForField(long sizeSoFar,
java.lang.reflect.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 of
NUM_BYTES_OBJECT_ALIGNMENT . |
static java.lang.String |
humanReadableUnits(long bytes)
Returns
size in human-readable units (GB, MB, KB or bytes). |
static java.lang.String |
humanReadableUnits(long bytes,
java.text.DecimalFormat df)
Returns
size in human-readable units (GB, MB, KB or bytes). |
static long |
shallowSizeOf(java.lang.Object obj)
Estimates a "shallow" memory usage of the given object.
|
static long |
shallowSizeOf(java.lang.Object[] arr)
Returns the shallow size in bytes of the Object[] object.
|
private static long |
shallowSizeOfArray(java.lang.Object array)
Return shallow size of any
array . |
static long |
shallowSizeOfInstance(java.lang.Class<?> clazz)
Returns the shallow instance size in bytes an instance of the given class would occupy.
|
static long |
sizeOf(Accountable accountable)
Returns the size in bytes of the
Accountable object, using its
Accountable.ramBytesUsed() method. |
static long |
sizeOf(Accountable[] accountables)
Return the size of the provided array of
Accountable s by summing
up the shallow size of the array and the
memory usage reported by each
Accountable . |
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(java.lang.Long value)
Return the size of the provided
Long object, returning 0 if it is
cached by the JVM and its shallow size otherwise. |
static long |
sizeOf(long[] arr)
Returns the size in bytes of the long[] 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(short[] arr)
Returns the size in bytes of the short[] object.
|
static long |
sizeOf(java.lang.String s)
Returns the size in bytes of the String object.
|
static long |
sizeOf(java.lang.String[] arr)
Returns the size in bytes of the String[] object.
|
static long |
sizeOfCollection(java.util.Collection<?> collection)
Returns the size in bytes of a Collection object, including sizes of its values, supplying
UNKNOWN_DEFAULT_RAM_BYTES_USED when object type is not well known. |
private static long |
sizeOfCollection(java.util.Collection<?> collection,
int depth,
long defSize) |
static long |
sizeOfCollection(java.util.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(java.util.Map<?,?> map)
Returns the size in bytes of a Map object, including sizes of its keys and values, supplying
UNKNOWN_DEFAULT_RAM_BYTES_USED when object type is not well known. |
private static long |
sizeOfMap(java.util.Map<?,?> map,
int depth,
long defSize) |
static long |
sizeOfMap(java.util.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(java.lang.Object o)
Best effort attempt to estimate the size in bytes of an undetermined object.
|
private static long |
sizeOfObject(java.lang.Object o,
int depth,
long defSize) |
static long |
sizeOfObject(java.lang.Object o,
long defSize)
Best effort attempt to estimate the size in bytes of an undetermined object.
|
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static final boolean COMPRESSED_REFS_ENABLED
public static final int NUM_BYTES_OBJECT_REF
public static final int NUM_BYTES_OBJECT_HEADER
public static final int NUM_BYTES_ARRAY_HEADER
public static final int NUM_BYTES_OBJECT_ALIGNMENT
public static final int QUERY_DEFAULT_RAM_BYTES_USED
public static final int UNKNOWN_DEFAULT_RAM_BYTES_USED
public static final java.util.Map<java.lang.Class<?>,java.lang.Integer> primitiveSizes
static final long LONG_CACHE_MIN_VALUE
static final long LONG_CACHE_MAX_VALUE
static final int LONG_SIZE
static final int STRING_SIZE
static final boolean JVM_IS_HOTSPOT_64BIT
static final java.lang.String MANAGEMENT_FACTORY_CLASS
static final java.lang.String HOTSPOT_BEAN_CLASS
public static final long HASHTABLE_RAM_BYTES_PER_ENTRY
public static final long LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY
public static final int MAX_DEPTH
public static long alignObjectSize(long size)
NUM_BYTES_OBJECT_ALIGNMENT
.public static long sizeOf(java.lang.Long value)
Long
object, returning 0 if it is
cached by the JVM and its shallow size otherwise.public static long sizeOf(byte[] arr)
public static long sizeOf(boolean[] arr)
public static long sizeOf(char[] arr)
public static long sizeOf(short[] arr)
public static long sizeOf(int[] arr)
public static long sizeOf(float[] arr)
public static long sizeOf(long[] arr)
public static long sizeOf(double[] arr)
public static long sizeOf(java.lang.String[] arr)
public static long sizeOfMap(java.util.Map<?,?> map)
UNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known.
This method recurses up to MAX_DEPTH
.public static long sizeOfMap(java.util.Map<?,?> map, long defSize)
MAX_DEPTH
.private static long sizeOfMap(java.util.Map<?,?> map, int depth, long defSize)
public static long sizeOfCollection(java.util.Collection<?> collection)
UNKNOWN_DEFAULT_RAM_BYTES_USED
when object type is not well known.
This method recurses up to MAX_DEPTH
.public static long sizeOfCollection(java.util.Collection<?> collection, long defSize)
MAX_DEPTH
.private static long sizeOfCollection(java.util.Collection<?> collection, int depth, long defSize)
public static long sizeOf(Query q)
QUERY_DEFAULT_RAM_BYTES_USED
.public static long sizeOf(Query q, long defSize)
shallowSizeOf(Object)
, or using the supplied defSize
parameter
if its value is greater than 0.public static long sizeOfObject(java.lang.Object o)
UNKNOWN_DEFAULT_RAM_BYTES_USED
.public static long sizeOfObject(java.lang.Object o, long defSize)
shallowSizeOf(Object)
, or using the supplied defSize
parameter if
its value is greater than 0.private static long sizeOfObject(java.lang.Object o, int depth, long defSize)
public static long sizeOf(Accountable accountable)
Accountable
object, using its
Accountable.ramBytesUsed()
method.public static long sizeOf(java.lang.String s)
public static long shallowSizeOf(java.lang.Object[] arr)
public static long shallowSizeOf(java.lang.Object obj)
public static long shallowSizeOfInstance(java.lang.Class<?> clazz)
java.lang.IllegalArgumentException
- if clazz
is an array class.shallowSizeOf(Object)
private static long shallowSizeOfArray(java.lang.Object array)
array
.static long adjustForField(long sizeSoFar, java.lang.reflect.Field f)
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).
public static java.lang.String humanReadableUnits(long bytes)
size
in human-readable units (GB, MB, KB or bytes).public static java.lang.String humanReadableUnits(long bytes, java.text.DecimalFormat df)
size
in human-readable units (GB, MB, KB or bytes).public static long sizeOf(Accountable[] accountables)
Accountable
s by summing
up the shallow size of the array and the
memory usage
reported by each
Accountable
.