com.javamex.classmexer
Class MemoryUtil

java.lang.Object
  extended by com.javamex.classmexer.MemoryUtil

public class MemoryUtil
extends java.lang.Object

Provides various static methods for measuring the memory usage of objects. For these methods to work, the JVM must be started with classmexer.jar installed as an agent, for example:

 java -javaagent:classmexer.jar mypackage/MyClass
 


Nested Class Summary
static class MemoryUtil.VisibilityFilter
           
 
Method Summary
static long deepMemoryUsageOf(java.lang.Object obj)
          Returns an estimation, in bytes, of the memory usage of the given object plus (recursively) objects it references via non-static private or protected fields.
static long deepMemoryUsageOf(java.lang.Object obj, MemoryUtil.VisibilityFilter referenceFilter)
          Returns an estimation, in bytes, of the memory usage of the given object plus (recursively) objects it references via non-static references.
static long deepMemoryUsageOfAll(java.util.Collection<? extends java.lang.Object> objs)
          Returns an estimation, in bytes, of the memory usage of the given objects plus (recursively) objects referenced via non-static references from any of those objects via non-public fields.
static long deepMemoryUsageOfAll(java.util.Collection<? extends java.lang.Object> objs, MemoryUtil.VisibilityFilter referenceFilter)
          Returns an estimation, in bytes, of the memory usage of the given objects plus (recursively) objects referenced via non-static references from any of those objects.
static long memoryUsageOf(java.lang.Object obj)
          Returns an estimation of the "shallow" memory usage, in bytes, of the given object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

memoryUsageOf

public static long memoryUsageOf(java.lang.Object obj)
Returns an estimation of the "shallow" memory usage, in bytes, of the given object.

The estimate is provided by the running JVM and is likely to be as accurate a measure as can be reasonably made by the running Java program. It will generally include memory taken up for "housekeeping" of that object.

The shallow memory usage does not count the memory used by objects referenced by obj.

Parameters:
obj - The object whose memory usage is to be estimated.
Returns:
An estimate, in bytes, of the heap memory taken up by obj.
Throws:
java.lang.IllegalStateException - If the Agent wasn't installed when the JVM was started up.

deepMemoryUsageOf

public static long deepMemoryUsageOf(java.lang.Object obj)
Returns an estimation, in bytes, of the memory usage of the given object plus (recursively) objects it references via non-static private or protected fields.

The estimate for each individual object is provided by the running JVM and is likely to be as accurate a measure as can be reasonably made by the running Java program. It will generally include memory taken up for "housekeeping" of that object.

Optionally,

Parameters:
obj - The object whose memory usage (and that of objects it references) is to be estimated.
Returns:
An estimate, in bytes, of the heap memory taken up by obj and objects it references via private or protected non-static fields.
Throws:
java.lang.SecurityException - If the field definitions of obj cannot be read via reflection for the purpose of traversing its object graph.
java.lang.IllegalStateException - If the Agent wasn't installed when the JVM was started up.

deepMemoryUsageOf

public static long deepMemoryUsageOf(java.lang.Object obj,
                                     MemoryUtil.VisibilityFilter referenceFilter)
Returns an estimation, in bytes, of the memory usage of the given object plus (recursively) objects it references via non-static references. Which references are traversed depends on the VisibilityFilter passed in.

The estimate for each individual object is provided by the running JVM and is likely to be as accurate a measure as can be reasonably made by the running Java program. It will generally include memory taken up for "housekeeping" of that object.

Parameters:
obj - The object whose memory usage (and that of objects it references) is to be estimated.
referenceFilter - Specifies which references are to be recursively included in the resulting sount.
Returns:
An estimate, in bytes, of the heap memory taken up by obj and objects it references.
Throws:
java.lang.SecurityException - If the field definitions of obj cannot be read via reflection for the purpose of traversing its object graph.
java.lang.IllegalStateException - If the Agent wasn't installed when the JVM was started up.

deepMemoryUsageOfAll

public static long deepMemoryUsageOfAll(java.util.Collection<? extends java.lang.Object> objs)
Returns an estimation, in bytes, of the memory usage of the given objects plus (recursively) objects referenced via non-static references from any of those objects via non-public fields.

If two or more of the given objects reference the same Object X, then the memory used by Object X will only be counted once. However, the method guarantees that the memory for a given object (either in the passed-in collection or found while traversing the object graphs from those objects) will not be counted more than once.

The estimate for each individual object is provided by the running JVM and is likely to be as accurate a measure as can be reasonably made by the running Java program. It will generally include memory taken up for "housekeeping" of that object.

Parameters:
objs - The collection of objects whose memory usage is to be totalled.
Returns:
An estimate, in bytes, of the total heap memory taken up by the obejcts in objs and, recursively, objects referenced by private or protected (non-static) fields.
Throws:
java.lang.SecurityException - If the field definitions of any objects in objs cannot be read via reflection for the purpose of traversing the corresponding object graph.
java.lang.IllegalStateException - If the Agent wasn't installed when the JVM was started up.

deepMemoryUsageOfAll

public static long deepMemoryUsageOfAll(java.util.Collection<? extends java.lang.Object> objs,
                                        MemoryUtil.VisibilityFilter referenceFilter)
Returns an estimation, in bytes, of the memory usage of the given objects plus (recursively) objects referenced via non-static references from any of those objects. Which references are traversed depends on the VisibilityFilter passed in.

If two or more of the given objects reference the same Object X, then the memory used by Object X will only be counted once. However, the method guarantees that the memory for a given object (either in the passed-in collection or found while traversing the object graphs from those objects) will not be counted more than once.

The estimate for each individual object is provided by the running JVM and is likely to be as accurate a measure as can be reasonably made by the running Java program. It will generally include memory taken up for "housekeeping" of that object.

Parameters:
objs - The collection of objects whose memory usage is to be totalled.
referenceFilter - Specifies which references are to be recursively included in the resulting count.
Returns:
An estimate, in bytes, of the total heap memory taken up by the obejcts in objs and, recursively, objects referenced by any of those objects that match the VisibilityFilter criterion.
Throws:
java.lang.SecurityException - If the field definitions of any objects in objs cannot be read via reflection for the purpose of traversing the corresponding object graph.
java.lang.IllegalStateException - If the Agent wasn't installed when the JVM was started up.