Package org.h2.util
Class MemoryEstimator
- java.lang.Object
-
- org.h2.util.MemoryEstimator
-
public final class MemoryEstimator extends java.lang.Object
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.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
COUNTER_MASK
private static int
INIT_BIT
private static int
INIT_BIT_SHIFT
private static int
MAGNITUDE_LIMIT
private static int
SKIP_SUM_MASK
private static int
SKIP_SUM_SHIFT
private static int
SUM_SHIFT
private static int
WINDOW_HALF_SIZE
private static int
WINDOW_SHIFT
private static int
WINDOW_SIZE
-
Constructor Summary
Constructors Modifier Constructor Description private
MemoryEstimator()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static int
calculateMagnitude(long sum, long absDelta)
private static long
constructStatsData(long sum, long initialized, int skipSum, int counter)
static <T> int
estimateMemory(java.util.concurrent.atomic.AtomicLong stats, DataType<T> dataType, T data)
Estimates memory size of the data based on previous values.static <T> int
estimateMemory(java.util.concurrent.atomic.AtomicLong stats, DataType<T> dataType, T[] storage, int 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)
static int
samplingPct(java.util.concurrent.atomic.AtomicLong stats)
Calculates percentage of how many times actual calculation happened (vs.private static long
updateStatsData(java.util.concurrent.atomic.AtomicLong stats, long statsData, int counter, int skipSum, long initialized, long sum, int itemsCount, int itemsMem)
private static long
updateStatsData(java.util.concurrent.atomic.AtomicLong stats, long statsData, long updatedStatsData, int itemsCount, int itemsMem)
-
-
-
Field Detail
-
SKIP_SUM_SHIFT
private static final int SKIP_SUM_SHIFT
- See Also:
- Constant Field Values
-
COUNTER_MASK
private static final int COUNTER_MASK
- See Also:
- Constant Field Values
-
SKIP_SUM_MASK
private static final int SKIP_SUM_MASK
- See Also:
- Constant Field Values
-
INIT_BIT_SHIFT
private static final int INIT_BIT_SHIFT
- See Also:
- Constant Field Values
-
INIT_BIT
private static final int INIT_BIT
- See Also:
- Constant Field Values
-
WINDOW_SHIFT
private static final int WINDOW_SHIFT
- See Also:
- Constant Field Values
-
MAGNITUDE_LIMIT
private static final int MAGNITUDE_LIMIT
- See Also:
- Constant Field Values
-
WINDOW_SIZE
private static final int WINDOW_SIZE
- See Also:
- Constant Field Values
-
WINDOW_HALF_SIZE
private static final int WINDOW_HALF_SIZE
- See Also:
- Constant Field Values
-
SUM_SHIFT
private static final int SUM_SHIFT
- See Also:
- Constant Field Values
-
-
Method Detail
-
estimateMemory
public static <T> int estimateMemory(java.util.concurrent.atomic.AtomicLong stats, DataType<T> dataType, T data)
Estimates memory size of the data based on previous values.- Type Parameters:
T
- type of the data- Parameters:
stats
- AtomicLong holding statistical data about the estimated sequencedataType
- used for calculation of the next sequence value, if necessarydata
- which size is to be calculated as the next sequence value, if necessary- Returns:
- next estimated or calculated value of the sequence
-
estimateMemory
public static <T> int estimateMemory(java.util.concurrent.atomic.AtomicLong stats, DataType<T> dataType, T[] storage, int count)
Estimates memory size of the data set based on previous values.- Type Parameters:
T
- type of the data in the storage- Parameters:
stats
- AtomicLong holding statistical data about the estimated sequencedataType
- used for calculation of the next sequence value, if necessarystorage
- of the data set, which size is to be calculatedcount
- number of data items in the storage- Returns:
- next estimated or calculated size of the storage
-
samplingPct
public static int samplingPct(java.util.concurrent.atomic.AtomicLong stats)
Calculates percentage of how many times actual calculation happened (vs. estimation)- Parameters:
stats
- AtomicLong holding statistical data about the estimated sequence- Returns:
- sampling percentage in range 0 - 100
-
calculateMagnitude
private static int calculateMagnitude(long sum, long absDelta)
-
updateStatsData
private static long updateStatsData(java.util.concurrent.atomic.AtomicLong stats, long statsData, int counter, int skipSum, long initialized, long sum, int itemsCount, int itemsMem)
-
constructStatsData
private static long constructStatsData(long sum, long initialized, int skipSum, int counter)
-
updateStatsData
private static long updateStatsData(java.util.concurrent.atomic.AtomicLong stats, long statsData, long updatedStatsData, int itemsCount, int itemsMem)
-
getCounter
private static int getCounter(long statsData)
-
getSkipSum
private static int getSkipSum(long statsData)
-
getAverage
private static int getAverage(long updatedStatsData)
-
-