Class SimpleMemoryCalculator
- All Implemented Interfaces:
ConfigurableCacheMap.UnitCalculator
- Direct Known Subclasses:
BinaryMemoryCalculator
,SimpleMapIndex.IndexCalculator
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):
- BigInteger
- BigDecimal
Binary
- Boolean
- Byte
- Character
- Date
- Double
- Float
- Integer
- Long
- Object
- String
- Time
- Timestamp
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
-
Nested Class Summary
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Map
An immutable map of fixed-sized classes to instance size (in bytes).static final Map
An immutable map of primitive types to their size (in bytes).protected static final int
The unaligned size of the simplest object.static final int
The size (in bytes) of aBigDecimal
object.static final int
The size (in bytes) of aBigInteger
object.static final int
The minimum size (in bytes) of aBinary
object.static final int
The size (in bytes) of aBoolean
object.static final int
The size (in bytes) of aByte
object.static final int
The size (in bytes) of aCharacter
object.static final int
static final int
The size (in bytes) of aDouble
object.static final int
The minimum size (in bytes) of anLocalCache.Entry
object.static final int
The size (in bytes) of aFloat
object.static final int
The size (in bytes) of aInteger
object.static final int
The size (in bytes) of aLong
object.static final int
The size (in bytes) of anObject
.static final int
The size (in bytes) of an object reference.static final int
The size (in bytes) of aShort
object.static final int
The minimum size (in bytes) of aString
object.static final int
The size (in bytes) of aTimestamp
object. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected static int
Calculate the approximate number of bytes required to store an instance of the given class and its non-static fields in memory.int
calculateUnits
(Object oKey, Object oValue) Calculate a cache cost for the specified cache entry key and value.protected int
Return the size of a map entry.getName()
Obtain the name of the unit calculator.static boolean
isInnerClass
(Class clz) Return true if a given class is an inner class.static void
Unit test.protected static int
padMemorySize
(int cb) Round the given number of bytes to the next closest integer that is divisible by 8.protected int
Determine the minimum number of bytes required to store an instance of the given fixed-sized class in memory.int
Estimate the number of bytes of memory consumed by the given object.Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
Field Details
-
SIZE_OBJECT_REF
public static final int SIZE_OBJECT_REFThe 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_OBJECTThe size (in bytes) of anObject
. -
SIZE_BOOLEAN
public static final int SIZE_BOOLEANThe size (in bytes) of aBoolean
object. -
SIZE_BYTE
public static final int SIZE_BYTEThe size (in bytes) of aByte
object. -
SIZE_SHORT
public static final int SIZE_SHORTThe size (in bytes) of aShort
object. -
SIZE_CHARACTER
public static final int SIZE_CHARACTERThe size (in bytes) of aCharacter
object. -
SIZE_INTEGER
public static final int SIZE_INTEGERThe size (in bytes) of aInteger
object. -
SIZE_FLOAT
public static final int SIZE_FLOATThe size (in bytes) of aFloat
object. -
SIZE_LONG
public static final int SIZE_LONGThe size (in bytes) of aLong
object. -
SIZE_DOUBLE
public static final int SIZE_DOUBLEThe size (in bytes) of aDouble
object. -
SIZE_DATE
public static final int SIZE_DATE -
SIZE_TIMESTAMP
public static final int SIZE_TIMESTAMPThe size (in bytes) of aTimestamp
object. -
SIZE_BIGINTEGER
public static final int SIZE_BIGINTEGERThe size (in bytes) of aBigInteger
object. -
SIZE_BIGDECIMAL
public static final int SIZE_BIGDECIMALThe size (in bytes) of aBigDecimal
object. -
SIZE_STRING
public static final int SIZE_STRINGThe minimum size (in bytes) of aString
object. -
SIZE_BINARY
public static final int SIZE_BINARYThe minimum size (in bytes) of aBinary
object. -
SIZE_ENTRY
public static final int SIZE_ENTRYThe minimum size (in bytes) of anLocalCache.Entry
object. -
MAP_FIXED_SIZES
An immutable map of fixed-sized classes to instance size (in bytes). -
MAP_PRIMITIVE_SIZES
An immutable map of primitive types to their size (in bytes). -
SIZE_BASIC_OBJECT
protected static final int SIZE_BASIC_OBJECTThe unaligned size of the simplest object.
-
-
Constructor Details
-
SimpleMemoryCalculator
public SimpleMemoryCalculator()
-
-
Method Details
-
calculateUnits
Calculate a cache cost for the specified cache entry key and value.- Specified by:
calculateUnits
in interfaceConfigurableCacheMap.UnitCalculator
- Parameters:
oKey
- the cache key to evaluate for unit costoValue
- the cache value to evaluate for unit cost- Returns:
- an integer value 0 or greater, with a larger value signifying a higher cost
-
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 interfaceConfigurableCacheMap.UnitCalculator
- Returns:
- the name of the unit calculator
-
getEntrySize
protected int getEntrySize()Return the size of a map entry.- Returns:
- the entry size
-
sizeOf
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 8-byte boundary:
Size of Various Types Class Size boolean 1 byte 1 short 2 char 2 int 4 long 8 float 4 double 8 Object SIZE_OBJECT
Boolean SIZE_OBJECT
+ 1Byte SIZE_OBJECT
+ 1Short SIZE_OBJECT
+ 2Character SIZE_OBJECT
+ 2Integer SIZE_OBJECT
+ 4Long SIZE_OBJECT
+ 8Float SIZE_OBJECT
+ 4Double SIZE_OBJECT
+ 8BigInteger SIZE_OBJECT
+ 48BigDecimal SIZE_OBJECT
+ 4 +SIZE_OBJECT_REF
+SIZE_BIGINTEGER
Date SIZE_OBJECT
+SIZE_OBJECT_REF
+ 8Time SIZE_OBJECT
+SIZE_OBJECT_REF
+ 8Timestamp SIZE_OBJECT
+SIZE_OBJECT_REF
+ 12Binary SIZE_BINARY
+ arrayString SIZE_STRING
+ arrayarray SIZE_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:
IllegalArgumentException
- if the type of the object is not one of the classes listed above
-
sizeOf
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:
Size of Various Types Class Size boolean 1 byte 1 short 2 char 2 int 4 long 8 float 4 double 8 Object SIZE_OBJECT
Boolean SIZE_OBJECT
+ 1Byte SIZE_OBJECT
+ 1Short SIZE_OBJECT
+ 2Character SIZE_OBJECT
+ 2Integer SIZE_OBJECT
+ 4Long SIZE_OBJECT
+ 8Float SIZE_OBJECT
+ 4Double SIZE_OBJECT
+ 8BigInteger SIZE_OBJECT
+ 48BigDecimal SIZE_OBJECT
+ 4 +SIZE_OBJECT_REF
+SIZE_BIGINTEGER
Date SIZE_OBJECT
+SIZE_OBJECT_REF
+ 8Time SIZE_OBJECT
+SIZE_OBJECT_REF
+ 8Timestamp SIZE_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:
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 8.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 8 bytes.
- Parameters:
cb
- the number of bytes to round- Returns:
- the input, rounded up to the nearest multiple of 8
-
calculateShallowSize
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
-
isInnerClass
Return true if a given class is an inner class.- Parameters:
clz
- the class to check- Returns:
- true if the provided class is an inner class
-
main
Unit test.Usage:
java com.tangosol.net.cache.SimpleMemoryCalculator [class name]
- Parameters:
asArg
- command line arguments
-