Package org.github.jamm

# 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.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static final class`
`MemoryMeter.Builder`
Builder for `MemoryMeter` instances
`static enum`
`MemoryMeter.ByteBufferMode`
The different way of measuring deeply a ByteBuffer.
`static enum`
`MemoryMeter.Guess`
The different strategies that can be used by a `MemoryMeter` instance to measure the shallow size of an object.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final List<MemoryMeter.Guess>`
`BEST`
The default guesses in accuracy order.

## Constructor Summary

Constructors
Constructor
Description
`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.

## Method Summary

Modifier and Type
Method
Description
`static void`
`agentmain(String options, Instrumentation inst)`
`static MemoryMeter.Builder`
`builder()`
`static MemoryLayoutSpecification`
`getMemoryLayoutSpecification()`
Provides information about the memory layout used by the JVM.
`static boolean`
`hasInstrumentation()`
`static boolean`
`hasUnsafe()`
`long`
`measure(Object object)`
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`
`measureStringDeep(String s)`
Measures the deep memory usage of the specified `String`
`static void`
`premain(String options, Instrumentation inst)`
`static boolean`
`useStringOptimization()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### BEST

public static final List<MemoryMeter.Guess> BEST
The default guesses in accuracy order.

## 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