Top Description Fields Constructors Methods
org.h2.util

public final Class MemoryEstimator

extends Object
Class Inheritance
Static Imports
org.h2.engine.Constants.MEMORY_POINTER

Class MemoryEstimator. Calculation of the amount of memory occupied by keys, values and pages of the MVTable may become expensive operation for complex data types like Row. On the other hand, result of the calculation is used by page cache to limit it's size and determine when eviction is needed. Another usage is to trigger auto commit, based on amount of unsaved changes. In both cases reasonable (lets say ~30%) approximation would be good enough and will do the job. This class replaces exact calculation with an estimate based on a sliding window average of last 256 values. If estimation gets close to the exact value, then next N calculations are skipped and replaced with the estimate, where N depends on the estimation error.
Author
Andrei Tokar

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static int
calculateMagnitude(long sum, long absDelta)

private static long
constructStatsData(long sum, long initialized, int skipSum, int counter)

public static <
type of the data
T
>
int

Returns:

next estimated or calculated value of the sequence
estimateMemory
(AtomicLong
AtomicLong holding statistical data about the estimated sequence
stats
,
DataType<T>
used for calculation of the next sequence value, if necessary
dataType
,
T
which size is to be calculated as the next sequence value, if necessary
data
)

Estimates memory size of the data based on previous values.

public static <
type of the data in the storage
T
>
int

Returns:

next estimated or calculated size of the storage
estimateMemory
(AtomicLong
AtomicLong holding statistical data about the estimated sequence
stats
,
DataType<T>
used for calculation of the next sequence value, if necessary
dataType
,
T[]
of the data set, which size is to be calculated
storage
,
int
number of data items in the storage
count
)

Estimates memory size of the data set based on previous values.

private static int
getAverage(long updatedStatsData)

private static int
getCounter(long statsData)

private static int
getSkipSum(long statsData)

public static int

Returns:

sampling percentage in range 0 - 100
samplingPct
(AtomicLong
AtomicLong holding statistical data about the estimated sequence
stats
)

Calculates percentage of how many times actual calculation happened (vs. estimation)

private static long
updateStatsData(AtomicLong stats, long statsData, int counter, int skipSum, long initialized, long sum, int itemsCount, int itemsMem)

private static long
updateStatsData(AtomicLong stats, long statsData, long updatedStatsData, int itemsCount, int itemsMem)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COUNTER_MASKback to summary
private static final int COUNTER_MASK
INIT_BITback to summary
private static final int INIT_BIT
INIT_BIT_SHIFTback to summary
private static final int INIT_BIT_SHIFT
MAGNITUDE_LIMITback to summary
private static final int MAGNITUDE_LIMIT
SKIP_SUM_MASKback to summary
private static final int SKIP_SUM_MASK
SKIP_SUM_SHIFTback to summary
private static final int SKIP_SUM_SHIFT
SUM_SHIFTback to summary
private static final int SUM_SHIFT
WINDOW_HALF_SIZEback to summary
private static final int WINDOW_HALF_SIZE
WINDOW_SHIFTback to summary
private static final int WINDOW_SHIFT
WINDOW_SIZEback to summary
private static final int WINDOW_SIZE

Constructor Detail

MemoryEstimatorback to summary
private MemoryEstimator()

Method Detail

calculateMagnitudeback to summary
private static int calculateMagnitude(long sum, long absDelta)
constructStatsDataback to summary
private static long constructStatsData(long sum, long initialized, int skipSum, int counter)
estimateMemoryback to summary
public static <T> int estimateMemory(AtomicLong stats, DataType<T> dataType, T data)

Estimates memory size of the data based on previous values.

Parameters
<T>
type of the data
stats:AtomicLong

AtomicLong holding statistical data about the estimated sequence

dataType:DataType<T>

used for calculation of the next sequence value, if necessary

data:T

which size is to be calculated as the next sequence value, if necessary

Returns:int

next estimated or calculated value of the sequence

estimateMemoryback to summary
public static <T> int estimateMemory(AtomicLong stats, DataType<T> dataType, T[] storage, int count)

Estimates memory size of the data set based on previous values.

Parameters
<T>
type of the data in the storage
stats:AtomicLong

AtomicLong holding statistical data about the estimated sequence

dataType:DataType<T>

used for calculation of the next sequence value, if necessary

storage:T[]

of the data set, which size is to be calculated

count:int

number of data items in the storage

Returns:int

next estimated or calculated size of the storage

getAverageback to summary
private static int getAverage(long updatedStatsData)
getCounterback to summary
private static int getCounter(long statsData)
getSkipSumback to summary
private static int getSkipSum(long statsData)
samplingPctback to summary
public static int samplingPct(AtomicLong stats)

Calculates percentage of how many times actual calculation happened (vs. estimation)

Parameters
stats:AtomicLong

AtomicLong holding statistical data about the estimated sequence

Returns:int

sampling percentage in range 0 - 100

updateStatsDataback to summary
private static long updateStatsData(AtomicLong stats, long statsData, int counter, int skipSum, long initialized, long sum, int itemsCount, int itemsMem)
updateStatsDataback to summary
private static long updateStatsData(AtomicLong stats, long statsData, long updatedStatsData, int itemsCount, int itemsMem)