Class MemoryMeter
This class supports multithreading and can be reused safely.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder forMemoryMeterinstancesstatic enumThe different way of measuring deeply a ByteBuffer.static enumThe different strategies that can be used by aMemoryMeterinstance 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 newMemoryMeterinstance from the different component it needs to measure object graph. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidagentmain(String options, Instrumentation inst) static MemoryMeter.Builderbuilder()static MemoryLayoutSpecificationProvides information about the memory layout used by the JVM.static booleanstatic booleanlongMeasures the shallow memory usage of the object.longmeasureArray(boolean[] array) Measures the shallow memory usage of the specified boolean array.longmeasureArray(byte[] array) Measures the shallow memory usage of the specified byte array.longmeasureArray(char[] array) Measures the shallow memory usage of the specified char array.longmeasureArray(double[] array) Measures the shallow memory usage of the specified double array.longmeasureArray(float[] array) Measures the shallow memory usage of the specified float array.longmeasureArray(int[] array) Measures the shallow memory usage of the specified int array.longmeasureArray(long[] array) Measures the shallow memory usage of the specified long array.longmeasureArray(short[] array) Measures the shallow memory usage of the specified short array.longmeasureArray(Object[] array) Measures the shallow memory usage of the specified Object array.longmeasureDeep(Object object) Measures the memory usage of the object including referenced objects.longmeasureDeep(Object object, MemoryMeter.ByteBufferMode bbMode) Measures the memory usage of the object including referenced objects.longMeasures the deep memory usage of the specifiedStringstatic voidpremain(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 newMemoryMeterinstance from the different component it needs to measure object graph.Unless there is a specific need to override some of the
MemoryMeterlogic people should only createMemoryMeterinstances 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- theMemoryMeterStrategyto 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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
nullthe 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- theStringto 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
nullthe value returned will be zero.Calling this method is equivalent to calling
measureDeep(object, ByteBufferMode)with aNORMALByteBufferMode.- 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
nullthe 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
-