Oracle® Coherence Java API Reference
v3.5.3

E15583-01

com.tangosol.net.cache
Class SimpleMemoryCalculator

java.lang.Object
  extended by com.tangosol.util.Base
      extended by com.tangosol.net.cache.SimpleMemoryCalculator
All Implemented Interfaces:
ConfigurableCacheMap.UnitCalculator, OldCache.UnitCalculator
Direct Known Subclasses:
BinaryMemoryCalculator

public class SimpleMemoryCalculator
extends Base
implements OldCache.UnitCalculator

A UnitCalculator implementation that weighs a cache entry based upon the amount of physical memory (in bytes) required to store the entry.

This implementation can only determine an accurate entry size if both the entry key and value object types are one of the following classes (or an array thereof or of the primitive forms thereof):

If either the key or value object is not one of these types, an exception will be thrown during the unit calculation.

Author:
jh 2005.12.20

Field Summary
static java.util.Map MAP_FIXED_SIZES
          An immutable map of fixed-sized classes to instance size (in bytes).
protected static int SIZE_BASIC_OBJECT
          The unalligned size of the simplest object.
static int SIZE_BIGDECIMAL
          The size (in bytes) of a BigDecimal object.
static int SIZE_BIGINTEGER
          The size (in bytes) of a BigInteger object.
static int SIZE_BINARY
          The minimum size (in bytes) of a Binary object.
static int SIZE_BOOLEAN
          The size (in bytes) of a Boolean object.
static int SIZE_BYTE
          The size (in bytes) of a Byte object.
static int SIZE_CHARACTER
          The size (in bytes) of a Character object.
static int SIZE_DATE
          The size (in bytes) of a Date, Date or Time object.
static int SIZE_DOUBLE
          The size (in bytes) of a Double object.
static int SIZE_ENTRY
          The minimum size (in bytes) of an LocalCache.Entry object.
static int SIZE_FLOAT
          The size (in bytes) of a Float object.
static int SIZE_INTEGER
          The size (in bytes) of a Integer object.
static int SIZE_LONG
          The size (in bytes) of a Long object.
static int SIZE_OBJECT
          The size (in bytes) of an Object.
static int SIZE_OBJECT_REF
          The size (in bytes) of an object reference.
static int SIZE_SHORT
          The size (in bytes) of a Short object.
static int SIZE_STRING
          The minimum size (in bytes) of a String object.
static int SIZE_TIMESTAMP
          The size (in bytes) of a Timestamp object.
 
Constructor Summary
SimpleMemoryCalculator()
           
 
Method Summary
protected static int calculateShallowSize(java.lang.Class clz)
          Calculate the approximate number of bytes required to store an instance of the given class and its non-static fields in memory.
 int calculateUnits(java.lang.Object oKey, java.lang.Object oValue)
          Calculate a cache cost for the specified cache entry key and value.
 java.lang.String getName()
          Obtain the name of the unit calculator.
static void main(java.lang.String[] asArg)
          Unit test.
protected static int padMemorySize(int cb)
          Round the given number of bytes to the next closest integer that is divisible by 16.
protected  int sizeOf(java.lang.Class clz)
          Determine the minimum number of bytes required to store an instance of the given fixed-sized class in memory.
protected  int sizeOf(java.lang.Object o)
          Estimate the number of bytes of memory consumed by the given object.
 

Field Detail

SIZE_OBJECT_REF

public static final int SIZE_OBJECT_REF
The size (in bytes) of an object reference.

On a 32 bit JVM, an object reference is 4 bytes. On a 64 bit JVM, an object reference is 8 bytes.


SIZE_OBJECT

public static final int SIZE_OBJECT
The size (in bytes) of an Object.


SIZE_BOOLEAN

public static final int SIZE_BOOLEAN
The size (in bytes) of a Boolean object.


SIZE_BYTE

public static final int SIZE_BYTE
The size (in bytes) of a Byte object.


SIZE_SHORT

public static final int SIZE_SHORT
The size (in bytes) of a Short object.


SIZE_CHARACTER

public static final int SIZE_CHARACTER
The size (in bytes) of a Character object.


SIZE_INTEGER

public static final int SIZE_INTEGER
The size (in bytes) of a Integer object.


SIZE_FLOAT

public static final int SIZE_FLOAT
The size (in bytes) of a Float object.


SIZE_LONG

public static final int SIZE_LONG
The size (in bytes) of a Long object.


SIZE_DOUBLE

public static final int SIZE_DOUBLE
The size (in bytes) of a Double object.


SIZE_DATE

public static final int SIZE_DATE
The size (in bytes) of a Date, Date or Time object.


SIZE_TIMESTAMP

public static final int SIZE_TIMESTAMP
The size (in bytes) of a Timestamp object.


SIZE_BIGINTEGER

public static final int SIZE_BIGINTEGER
The size (in bytes) of a BigInteger object.


SIZE_BIGDECIMAL

public static final int SIZE_BIGDECIMAL
The size (in bytes) of a BigDecimal object.


SIZE_STRING

public static final int SIZE_STRING
The minimum size (in bytes) of a String object.


SIZE_BINARY

public static final int SIZE_BINARY
The minimum size (in bytes) of a Binary object.


SIZE_ENTRY

public static final int SIZE_ENTRY
The minimum size (in bytes) of an LocalCache.Entry object.


MAP_FIXED_SIZES

public static final java.util.Map MAP_FIXED_SIZES
An immutable map of fixed-sized classes to instance size (in bytes).


SIZE_BASIC_OBJECT

protected static final int SIZE_BASIC_OBJECT
The unalligned size of the simplest object.

Constructor Detail

SimpleMemoryCalculator

public SimpleMemoryCalculator()
Method Detail

calculateUnits

public int calculateUnits(java.lang.Object oKey,
                          java.lang.Object oValue)
Calculate a cache cost for the specified cache entry key and value.

Specified by:
calculateUnits in interface ConfigurableCacheMap.UnitCalculator
Parameters:
oKey - the cache key to evaluate for unit cost
oValue - the cache value to evaluate for unit cost
Returns:
an integer value 0 or greater, with a larger value signifying a higher cost

getName

public java.lang.String getName()
Obtain the name of the unit calculator. This is intended to be human readable for use in a monitoring tool; examples include "SimpleMemoryCalculator" and "BinaryMemoryCalculator".

Specified by:
getName in interface ConfigurableCacheMap.UnitCalculator
Returns:
the name of the unit calculator

sizeOf

protected int sizeOf(java.lang.Object o)
Estimate the number of bytes of memory consumed by the given object.

The calculation is based on the class of the given object, with reference types assumed to be aligned on a 16-byte boundary:

ClassSize
boolean1
byte1
short2
char2
int4
long8
float4
double8
ObjectSIZE_OBJECT
BooleanSIZE_OBJECT + 1
ByteSIZE_OBJECT + 1
ShortSIZE_OBJECT + 2
CharacterSIZE_OBJECT + 2
IntegerSIZE_OBJECT + 4
LongSIZE_OBJECT + 8
FloatSIZE_OBJECT + 4
DoubleSIZE_OBJECT + 8
BigIntegerSIZE_OBJECT + 48
BigDecimalSIZE_OBJECT + 4 + SIZE_OBJECT_REF + SIZE_BIGINTEGER
DateSIZE_OBJECT + SIZE_OBJECT_REF + 8
TimeSIZE_OBJECT + SIZE_OBJECT_REF + 8
TimestampSIZE_OBJECT + SIZE_OBJECT_REF + 12
BinarySIZE_BINARY + array
StringSIZE_STRING + array
arraySIZE_OBJECT + 4 + [element size]*length()

Parameters:
o - the object to measure the size of
Returns:
an estimate of the number of bytes required to store the given object in memory
Throws:
java.lang.IllegalArgumentException - if the type of the object is not one of the classes listed above

sizeOf

protected int sizeOf(java.lang.Class clz)
Determine the minimum number of bytes required to store an instance of the given fixed-sized class in memory.

The calculation is based on the following table, with reference types assumed to be aligned on an 16-byte boundary:

ClassSize
boolean1
byte1
short2
char2
int4
long8
float4
double8
ObjectSIZE_OBJECT
BooleanSIZE_OBJECT + 1
ByteSIZE_OBJECT + 1
ShortSIZE_OBJECT + 2
CharacterSIZE_OBJECT + 2
IntegerSIZE_OBJECT + 4
LongSIZE_OBJECT + 8
FloatSIZE_OBJECT + 4
DoubleSIZE_OBJECT + 8
BigIntegerSIZE_OBJECT + 48
BigDecimalSIZE_OBJECT + 4 + SIZE_OBJECT_REF + SIZE_BIGINTEGER
DateSIZE_OBJECT + SIZE_OBJECT_REF + 8
TimeSIZE_OBJECT + SIZE_OBJECT_REF + 8
TimestampSIZE_OBJECT + SIZE_OBJECT_REF + 12

Parameters:
clz - the target class
Returns:
the minimum number of bytes required to store an instance of the given class in memory
Throws:
java.lang.IllegalArgumentException - if the type of the object is not one of the classes listed above

padMemorySize

protected static int padMemorySize(int cb)
Round the given number of bytes to the next closest integer that is divisible by 16.

This method is used to pad the result of a memory calculation assuming that fields are ordered to minimize padding and that allocation granularity is 16 bytes.

Parameters:
cb - the number of bytes to round
Returns:
the input, rounded up to the nearest multiple of 16

calculateShallowSize

protected static int calculateShallowSize(java.lang.Class clz)
Calculate the approximate number of bytes required to store an instance of the given class and its non-static fields in memory.

The size calculation is shallow, in that fields that are references to variable-sized classes are not included in the estimate.

Parameters:
clz - the target class
Returns:
the number of bytes required to store an instance of the given class, including its non-static members

main

public static void main(java.lang.String[] asArg)
Unit test.

Usage:

 java com.tangosol.net.cache.SimpleMemoryCalculator [class name]
 

Parameters:
asArg - command line arguments

Oracle® Coherence Java API Reference
v3.5.3

E15583-01

Copyright © 2000, 2010, Oracle. All rights reserved.