bb.util
Class MemoryMeasurer

java.lang.Object
  extended by bb.util.MemoryMeasurer

public class MemoryMeasurer
extends Object

Class which measures memory state in a JVM.

This class is multithread safe: it is stateless.

Author:
Brent Boyer

Nested Class Summary
static class MemoryMeasurer.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  boolean callRestoreJvm
          If true, causes getMemoryState to first call restoreJvm before measuring memory.
private static int maxRestoreJvmLoops
          Specifies the maximum number of loops that restoreJvm will execute.
private static long pauseTime
          Specifies the number of milliseconds that restoreJvm will sleep for between loop iterations.
 
Constructor Summary
MemoryMeasurer()
          Calls this(false).
MemoryMeasurer(boolean callRestoreJvm)
          Fundamental constructor.
 
Method Summary
 MemoryState getMemoryState()
          Returns a new MemoryState instance which describes the current memory state.
static long memoryUsed()
          Returns how much memory on the heap is currently being used.
static MemoryState perform()
          Returns perform(false).
static MemoryState perform(boolean callRestoreJvm)
          Returns new MemoryMeasurer(callRestoreJvm).
static void restoreJvm()
          Tries to restore the JVM to as clean a state as possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxRestoreJvmLoops

private static final int maxRestoreJvmLoops
Specifies the maximum number of loops that restoreJvm will execute.

See Also:
Constant Field Values

pauseTime

private static final long pauseTime
Specifies the number of milliseconds that restoreJvm will sleep for between loop iterations.

See Also:
Constant Field Values

callRestoreJvm

private final boolean callRestoreJvm
If true, causes getMemoryState to first call restoreJvm before measuring memory.

Constructor Detail

MemoryMeasurer

public MemoryMeasurer()
Calls this(false).


MemoryMeasurer

public MemoryMeasurer(boolean callRestoreJvm)
Fundamental constructor.

Method Detail

perform

public static MemoryState perform()
Returns perform(false).


perform

public static MemoryState perform(boolean callRestoreJvm)
Returns new MemoryMeasurer(callRestoreJvm).getMemoryState(). This is a convenience method for users who just want a few memory measurements. Users who wish to make many measurements should create a dedicated MemoryMeasurer instance instead.


restoreJvm

public static void restoreJvm()
Tries to restore the JVM to as clean a state as possible.

The first technique is a request for object finalizers to run (via a call to System.runFinalization). The second technique is a request for garbage collection to run (via a call to System.gc).

These calls are done in a loop that executes at least once, and at most maxRestoreJvmLoops times, but will execute fewer times if no more objects remain to be finalized and heap memory usage becomes constant.

This article suggested the idea to aggressively call for garbage collection many times, and to use heap memory as a metric for deciding when can stop garbage collecting.


memoryUsed

public static long memoryUsed()
Returns how much memory on the heap is currently being used.


getMemoryState

public MemoryState getMemoryState()
Returns a new MemoryState instance which describes the current memory state.

No guarantee can be made regarding the accuracy of the result since it is impossible to get an atomic snapshot of the JVM's memory state. Furthermore, the memory state can change rapidly, so that the result may soon be obsolete.

See Also:
callRestoreJvm