uk.org.ogsadai.common
Class MemoryUtilities

java.lang.Object
  |
  +--uk.org.ogsadai.common.MemoryUtilities

public class MemoryUtilities
extends java.lang.Object

Utilities for managing, checking and logging memory usage.

Author:
The OGSA-DAI Project Team.

Field Summary
private static java.lang.String COPYRIGHT_NOTICE
           
private static DAILogger LOG
           
private static long MAX_MEMORY
           
private static java.lang.Runtime RUNTIME
           
static double THRESHOLD
          Threshold ratio for how full the JVM memory must be before an exception is thrown (to avoid a java.lang.OutOfMemoryError).
 
Constructor Summary
MemoryUtilities()
           
 
Method Summary
static void checkMemory(byte[] bytes, DAILogger log)
          Check that the copying of a byte array within the JVM will not go over the threshold memory ratio set for this system.
static void checkMemory(java.io.ByteArrayOutputStream stream, DAILogger log)
          Check that the convertion of a ByteArrayOutputStream to a byte array within the JVM will not go over the threshold memory ratio set for this system.
static void checkMemory(int numberOfBytesToAdd, DAILogger log)
          Check that the allocation of an extra number of bytes within the JVM will not go over the threshold ratio set for this system.
static void checkMemory(java.lang.StringBuffer buffer, DAILogger log)
          Check that the conversion of a StringBuffer to a String within the JVM will not go over the threshold memory ratio set for this system.
static void checkMemory(java.lang.String stringToCopy, DAILogger log)
          Check that the copying of a String within the JVM will not go over the threshold memory ratio set for this system.
private static long getCurrentlyUsedMemory()
          Get current memory in use.
private static double getCurrentRatio(int numberOfBytesToAdd)
          Get ratio of current memory in use + number of requested bytes to total memory.
static void logMemoryUsage(DAILogger log)
          Logs the current state of the JVM memory at DEBUG level to this object's own log and in the logger provided.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

LOG

private static final DAILogger LOG

RUNTIME

private static final java.lang.Runtime RUNTIME

MAX_MEMORY

private static final long MAX_MEMORY

THRESHOLD

public static final double THRESHOLD
Threshold ratio for how full the JVM memory must be before an exception is thrown (to avoid a java.lang.OutOfMemoryError).

See Also:
Constant Field Values
Constructor Detail

MemoryUtilities

public MemoryUtilities()
Method Detail

checkMemory

public static void checkMemory(java.lang.String stringToCopy,
                               DAILogger log)
                        throws OutOfMemoryException
Check that the copying of a String within the JVM will not go over the threshold memory ratio set for this system.

Parameters:
stringToCopy - The string to be copied.
log - Destination of memory usage info.
Throws:
OutOfMemoryException - if the ratio of used memory would be exceeded by allocating the number of bytes specified.

checkMemory

public static void checkMemory(byte[] bytes,
                               DAILogger log)
                        throws OutOfMemoryException
Check that the copying of a byte array within the JVM will not go over the threshold memory ratio set for this system.

Parameters:
bytes - The byte array to be copied.
log - Destination of memory usage info.
Throws:
OutOfMemoryException - if the ratio of used memory would be exceeded by allocating the number of bytes specified.

checkMemory

public static void checkMemory(java.lang.StringBuffer buffer,
                               DAILogger log)
                        throws OutOfMemoryException
Check that the conversion of a StringBuffer to a String within the JVM will not go over the threshold memory ratio set for this system.

Parameters:
buffer - The java.lang.StringBuffer to be converted.
log - Destination of memory usage info.
Throws:
OutOfMemoryException - if the ratio of used memory would be exceeded by allocating the number of bytes specified.

checkMemory

public static void checkMemory(java.io.ByteArrayOutputStream stream,
                               DAILogger log)
                        throws OutOfMemoryException
Check that the convertion of a ByteArrayOutputStream to a byte array within the JVM will not go over the threshold memory ratio set for this system.

Parameters:
stream - The java.io.ByteArrayOutputStream to be converted.
log - Destination of memory usage info.
Throws:
OutOfMemoryException - if the ratio of used memory would be exceeded by allocating the number of bytes specified.

checkMemory

public static void checkMemory(int numberOfBytesToAdd,
                               DAILogger log)
                        throws OutOfMemoryException
Check that the allocation of an extra number of bytes within the JVM will not go over the threshold ratio set for this system.

Parameters:
numberOfBytesToAdd - Number of bytes of memory to be tested prior to allocation
log - Destination of memory usage info.
Throws:
OutOfMemoryException - if the ratio of used memory would be exceeded by allocating the number of bytes specified.

getCurrentRatio

private static double getCurrentRatio(int numberOfBytesToAdd)
Get ratio of current memory in use + number of requested bytes to total memory.

Parameters:
numberOfBytesToAdd - Requested number of bytes.
Returns:
ratio.

getCurrentlyUsedMemory

private static long getCurrentlyUsedMemory()
Get current memory in use.

Returns:
current memory in use.

logMemoryUsage

public static void logMemoryUsage(DAILogger log)
Logs the current state of the JVM memory at DEBUG level to this object's own log and in the logger provided.

For analysis purposes, the format of the log message is as follows: [NUMBER] of [NUMBER] bytes in use. [NUMBER]%

Parameters:
log - Destination for log messages.