bb.util
Class MemoryState

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

public class MemoryState
extends Object

Class which records data describing the memory state of a JVM.

This class is multithread safe: it is mostly immutable (both its immediate state, as well as the deep state of its fields). The sole exception is the integerFormat field, which is guarded by synchronization.

Author:
Brent Boyer

Nested Class Summary
static class MemoryState.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  long available
          Amount of memory (within the max allocatable) that could be allocated.
private  long free
          Amount of memory (within the total allocated) that is free.
private static DecimalFormat integerFormat
           
private  long max
          Maximum amount of memory that could be used by the JVM.
static String separatorDefault
          Default value for the separator arg of toStringHeader(String)/toString(String, boolean).
private  long total
          Total amount of memory currently used by the JVM.
private  long used
          Amount of memory (within the total allocated) that is used.
 
Constructor Summary
MemoryState(long free, long total, long max)
          Constructor.
 
Method Summary
 boolean equals(Object obj)
          Determines equality based on whether or not obj is a MemoryState instance whose every field equals that of this instance.
private static String format(long value)
          Returns integerFormat.
 long getAvailable()
          Returns the value of the available field.
 double getAvailableRatio()
          Returns the ratio of the available field to the max field.
 long getFree()
          Returns the value of the free field.
 long getMax()
          Returns the value of the max field.
 long getTotal()
          Returns the value of the total field.
 long getUsed()
          Returns the value of the used field.
 double getUsedRatio()
          Returns the ratio of the used field to the max field.
 int hashCode()
          Returns a value based on all of the fields.
static MemoryState parse(String s, String separator)
          Parse a new MemoryState instance from the data in s.
 String toString()
          Returns toString(separatorDefault, true).
 String toString(String separator, boolean includeLabels)
          Returns a String which describes this instance.
static String toStringHeader()
          Returns toStringHeader(separatorDefault).
static String toStringHeader(String separator)
          Returns a description of the data returned by toString(String, boolean).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

separatorDefault

public static final String separatorDefault
Default value for the separator arg of toStringHeader(String)/toString(String, boolean).

See Also:
Constant Field Values

integerFormat

private static final DecimalFormat integerFormat

used

private final long used
Amount of memory (within the total allocated) that is used.

Contract: is >= 0, and is always equal to total - free


free

private final long free
Amount of memory (within the total allocated) that is free. This definition of free memory is the same used by Runtime.freeMemory().

Contract: is >= 0, and is always equal to total - used


available

private final long available
Amount of memory (within the max allocatable) that could be allocated.

Contract: is >= 0, and is always equal to max - used


total

private final long total
Total amount of memory currently used by the JVM. This definition of total memory is the same used by Runtime.totalMemory().

Contract: is >= 1, and is always equal to used + free


max

private final long max
Maximum amount of memory that could be used by the JVM. This definition of max memory is the same used by Runtime.maxMemory().

Contract: is >= 1, and is always equal to used + available

Constructor Detail

MemoryState

public MemoryState(long free,
                   long total,
                   long max)
            throws IllegalArgumentException
Constructor.

Throws:
IllegalArgumentException - if free < 0; total < 1; max < 1
Method Detail

format

private static String format(long value)
Returns integerFormat.format(value).


parse

public static MemoryState parse(String s,
                                String separator)
                         throws IllegalArgumentException
Parse a new MemoryState instance from the data in s.

Warning: separator will be directly supplied as the regex argument to String.split, so you must ensure that it is a valid regex and will split the tokens of s as expected. In particular, if s has field labels inside it, you will need to write a complicated regex to handle that, as opposed to a simple regex like ", " or "\\t" for bare comma and tab delimted data.

Throws:
IllegalArgumentException - if s is blank; separator == null; s does not obey the expected format for a legitimate MemoryState representation

getUsed

public long getUsed()
Returns the value of the used field.


getUsedRatio

public double getUsedRatio()
Returns the ratio of the used field to the max field. In other words, this method returns the relative amount of memory being used.


getFree

public long getFree()
Returns the value of the free field.


getAvailable

public long getAvailable()
Returns the value of the available field.


getAvailableRatio

public double getAvailableRatio()
Returns the ratio of the available field to the max field. In other words, this method returns the relative amount of memory that still could be allocated given the memory that is already being used.


getTotal

public long getTotal()
Returns the value of the total field.


getMax

public long getMax()
Returns the value of the max field.


equals

public final boolean equals(Object obj)
Determines equality based on whether or not obj is a MemoryState instance whose every field equals that of this instance.

Overrides:
equals in class Object

hashCode

public final int hashCode()
Returns a value based on all of the fields.

Overrides:
hashCode in class Object

toStringHeader

public static String toStringHeader()
Returns toStringHeader(separatorDefault).


toStringHeader

public static String toStringHeader(String separator)
                             throws IllegalArgumentException
Returns a description of the data returned by toString(String, boolean).

Throws:
IllegalArgumentException - if separator == null or separator.length() == 0

toString

public String toString()
Returns toString(separatorDefault, true).

Overrides:
toString in class Object

toString

public String toString(String separator,
                       boolean includeLabels)
                throws IllegalArgumentException
Returns a String which describes this instance.

Parameters:
separator - String value to place between the different memory types in order to separate them
includeLabels - specifies whether or not to label the different memory types
Throws:
IllegalArgumentException - if separator == null or separator.length() == 0