Class MemoryMeter
This class supports multithreading and can be reused safely.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Builder forMemoryMeter
instancesstatic enum
The different way of measuring deeply a ByteBuffer.static enum
The different strategies that can be used by aMemoryMeter
instance to measure the shallow size of an object. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final List
<MemoryMeter.Guess> The default guesses in accuracy order. -
Constructor Summary
ConstructorsConstructorDescriptionMemoryMeter
(MemoryMeterStrategy strategy, FieldAndClassFilter classFilter, FieldFilter fieldFilter, MemoryMeterListener.Factory listenerFactory) Create a newMemoryMeter
instance from the different component it needs to measure object graph. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
agentmain
(String options, Instrumentation inst) static MemoryMeter.Builder
builder()
static MemoryLayoutSpecification
Provides information about the memory layout used by the JVM.static boolean
static boolean
long
Measures the shallow memory usage of the object.long
measureArray
(boolean[] array) Measures the shallow memory usage of the specified boolean array.long
measureArray
(byte[] array) Measures the shallow memory usage of the specified byte array.long
measureArray
(char[] array) Measures the shallow memory usage of the specified char array.long
measureArray
(double[] array) Measures the shallow memory usage of the specified double array.long
measureArray
(float[] array) Measures the shallow memory usage of the specified float array.long
measureArray
(int[] array) Measures the shallow memory usage of the specified int array.long
measureArray
(long[] array) Measures the shallow memory usage of the specified long array.long
measureArray
(short[] array) Measures the shallow memory usage of the specified short array.long
measureArray
(Object[] array) Measures the shallow memory usage of the specified Object array.long
measureDeep
(Object object) Measures the memory usage of the object including referenced objects.long
measureDeep
(Object object, MemoryMeter.ByteBufferMode bbMode) Measures the memory usage of the object including referenced objects.long
Measures the deep memory usage of the specifiedString
static void
premain
(String options, Instrumentation inst) static boolean
-
Field Details
-
BEST
The default guesses in accuracy order.
-
-
Constructor Details
-
MemoryMeter
public MemoryMeter(MemoryMeterStrategy strategy, FieldAndClassFilter classFilter, FieldFilter fieldFilter, MemoryMeterListener.Factory listenerFactory) Create a newMemoryMeter
instance from the different component it needs to measure object graph.Unless there is a specific need to override some of the
MemoryMeter
logic people should only createMemoryMeter
instances throughMemoryMeter.builder()
. This constructor provides a way to modify part of the logic being used by allowing to use specific implementations for the strategy or filters.- Parameters:
strategy
- theMemoryMeterStrategy
to use for measuring object shallow size.classFilter
- the filter used to filter out classes from the measured object graphfieldFilter
- the filter used to filter out fields from the measured object graphlistenerFactory
- the factory used to create the listener listening to the object graph traversal
-
-
Method Details
-
premain
-
agentmain
-
hasInstrumentation
public static boolean hasInstrumentation() -
hasUnsafe
public static boolean hasUnsafe() -
useStringOptimization
public static boolean useStringOptimization() -
builder
-
getMemoryLayoutSpecification
Provides information about the memory layout used by the JVM.- Returns:
- information about the memory layout used by the JVM
-
measure
Measures the shallow memory usage of the object.If the object is
null
the value returned will be zero.- Parameters:
object
- the object to measure- Returns:
- the shallow memory usage of @param object
-
measureArray
Measures the shallow memory usage of the specified Object array.If the object is
null
the value returned will be zero.- Parameters:
array
- the object array to measure- Returns:
- the shallow memory usage of the array
-
measureArray
public long measureArray(byte[] array) Measures the shallow memory usage of the specified byte array.If the object is
null
the value returned will be zero.- Parameters:
array
- the byte array to measure- Returns:
- the shallow memory usage of the byte array
-
measureArray
public long measureArray(boolean[] array) Measures the shallow memory usage of the specified boolean array.If the object is
null
the value returned will be zero.- Parameters:
array
- the boolean array to measure- Returns:
- the shallow memory usage of the boolean array
-
measureArray
public long measureArray(short[] array) Measures the shallow memory usage of the specified short array.If the object is
null
the value returned will be zero.- Parameters:
array
- the short array to measure- Returns:
- the shallow memory usage of the short array
-
measureArray
public long measureArray(char[] array) Measures the shallow memory usage of the specified char array.If the object is
null
the value returned will be zero.- Parameters:
array
- the char array to measure- Returns:
- the shallow memory usage of the char array
-
measureArray
public long measureArray(int[] array) Measures the shallow memory usage of the specified int array.If the object is
null
the value returned will be zero.- Parameters:
array
- the int array to measure- Returns:
- the shallow memory usage of the int array
-
measureArray
public long measureArray(float[] array) Measures the shallow memory usage of the specified float array.If the object is
null
the value returned will be zero.- Parameters:
array
- the float array to measure- Returns:
- the shallow memory usage of the float array
-
measureArray
public long measureArray(double[] array) Measures the shallow memory usage of the specified double array.If the object is
null
the value returned will be zero.- Parameters:
array
- the double array to measure- Returns:
- the shallow memory usage of the double array
-
measureArray
public long measureArray(long[] array) Measures the shallow memory usage of the specified long array.If the object is
null
the value returned will be zero.- Parameters:
array
- the long array to measure- Returns:
- the shallow memory usage of the long array
-
measureStringDeep
Measures the deep memory usage of the specifiedString
- Parameters:
s
- theString
to measure- Returns:
- the deep memory usage of the specified string
-
measureDeep
Measures the memory usage of the object including referenced objects.If the object is
null
the value returned will be zero.Calling this method is equivalent to calling
measureDeep(object, ByteBufferMode)
with aNORMAL
ByteBufferMode
.- Parameters:
object
- the object to measure- Returns:
- the memory usage of @param object including referenced objects
-
measureDeep
Measures the memory usage of the object including referenced objects.If the object is
null
the value returned will be zero.- Parameters:
object
- the object to measurebbMode
- the mode that should be used to measure ByteBuffers.- Returns:
- the memory usage of @param object including referenced objects
-