Class MemoryMeter

java.lang.Object
org.github.jamm.MemoryMeter

public final class MemoryMeter extends Object
Utility to measure the heap space used by java objects.

This class supports multithreading and can be reused safely.

  • Field Details

  • Constructor Details

    • MemoryMeter

      public MemoryMeter(MemoryMeterStrategy strategy, FieldAndClassFilter classFilter, FieldFilter fieldFilter, MemoryMeterListener.Factory listenerFactory)
      Create a new MemoryMeter 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 create MemoryMeter instances through MemoryMeter.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 - the MemoryMeterStrategy to use for measuring object shallow size.
      classFilter - the filter used to filter out classes from the measured object graph
      fieldFilter - the filter used to filter out fields from the measured object graph
      listenerFactory - the factory used to create the listener listening to the object graph traversal
  • Method Details

    • premain

      public static void premain(String options, Instrumentation inst)
    • agentmain

      public static void agentmain(String options, Instrumentation inst)
    • hasInstrumentation

      public static boolean hasInstrumentation()
    • hasUnsafe

      public static boolean hasUnsafe()
    • useStringOptimization

      public static boolean useStringOptimization()
    • builder

      public static MemoryMeter.Builder builder()
    • getMemoryLayoutSpecification

      public static MemoryLayoutSpecification getMemoryLayoutSpecification()
      Provides information about the memory layout used by the JVM.
      Returns:
      information about the memory layout used by the JVM
    • measure

      public long measure(Object object)
      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

      public long measureArray(Object[] array)
      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

      public long measureStringDeep(String s)
      Measures the deep memory usage of the specified String
      Parameters:
      s - the String to measure
      Returns:
      the deep memory usage of the specified string
    • measureDeep

      public long measureDeep(Object object)
      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 a NORMAL ByteBufferMode.

      Parameters:
      object - the object to measure
      Returns:
      the memory usage of @param object including referenced objects
    • measureDeep

      public long measureDeep(Object object, MemoryMeter.ByteBufferMode bbMode)
      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 measure
      bbMode - the mode that should be used to measure ByteBuffers.
      Returns:
      the memory usage of @param object including referenced objects