public class ObjectSizeCache
extends java.lang.Object
java.util.HashMap.| Modifier and Type | Class and Description |
|---|---|
private static class |
ObjectSizeCache.Entry
Implements a bucket entry.
|
| Modifier and Type | Field and Description |
|---|---|
private static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity - MUST be a power of two.
|
private static float |
DEFAULT_LOAD_FACTOR
The load factor used when none specified in constructor.
|
static ObjectSizeCache |
INSTANCE
Stores the singleton instance of this class.
|
private float |
loadFactor
The load factor for the hash table.
|
private static int |
MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified
by either of the constructors with arguments.
|
private static IObjectSizeProvider |
objectSizeProvider
Stores the object size provider.
|
private int |
size
The number of key-value mappings contained in this map.
|
private ObjectSizeCache.Entry[] |
table
The table, resized as necessary.
|
private int |
threshold
The next size value at which to resize (capacity * load factor).
|
| Modifier | Constructor and Description |
|---|---|
private |
ObjectSizeCache()
Constructs an empty HashMap with the default initial capacity
(16) and the default load factor (0.75).
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
addEntry(int hash,
java.lang.Object key,
long size,
int bucketIndex)
Adds a new entry with the specified key, value and hash code to
the specified bucket.
|
void |
clear()
Removes all of the mappings from this map.
|
long |
getClassSize(java.lang.String className)
Returns the size of the class specified by
className. |
long |
getSize(java.lang.Object object,
boolean remember)
Returns the memory size of
object. |
(package private) static int |
hash(int key)
Applies a supplemental hash function to a given hashCode, which
defends against poor quality hash functions.
|
(package private) static int |
indexFor(int hash,
int length)
Returns index for hash code h.
|
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
void |
remove(java.lang.Object key)
Removes the mapping of
key to its memory size. |
(package private) void |
resize(int newCapacity)
Rehashes the contents of this map into a new array with a
larger capacity.
|
void |
setSize(java.lang.Object key,
long size)
Associates the specified value with the specified key in this map.
|
int |
size()
Returns the number of key-value mappings in this map.
|
(package private) void |
transfer(ObjectSizeCache.Entry[] newTable)
Transfers all entries from current table to newTable.
|
public static final ObjectSizeCache INSTANCE
private static IObjectSizeProvider objectSizeProvider
private static final int DEFAULT_INITIAL_CAPACITY
private static final int MAXIMUM_CAPACITY
private static final float DEFAULT_LOAD_FACTOR
private transient ObjectSizeCache.Entry[] table
private transient int size
private int threshold
private final float loadFactor
private ObjectSizeCache()
static int hash(int key)
key - the key to hashstatic int indexFor(int hash,
int length)
hash - the hash codelength - the length of the tablepublic void setSize(java.lang.Object key,
long size)
key - the name of the class or the Class to associate the size tosize - the memory size to be associated with
the classNamevoid addEntry(int hash,
java.lang.Object key,
long size,
int bucketIndex)
hash - the hash codekey - the class name of the class or the Classsize - the memory size of the class with classNamebucketIndex - the target bucket indexvoid resize(int newCapacity)
newCapacity - the new capacity, MUST be a power of two;
must be greater than current capacity unless current
capacity is MAXIMUM_CAPACITY (in which case value
is irrelevant).void transfer(ObjectSizeCache.Entry[] newTable)
newTable - the target tablepublic long getClassSize(java.lang.String className)
className.
Please note that this method is intended to be used while
instrumentation for improving the runtime performance of the memory size
determination.className - the name of the classclassName,
0 if the class is not known to this cache.public long getSize(java.lang.Object object,
boolean remember)
object. In case that the class
of object is already known to this cache, the stored size
is returned. Else the objectSizeProvider is used to determine
the size.object - the object to determine the size forremember - if true remember an unknown size,
false if an unknown size may be ignroedobjectpublic void remove(java.lang.Object key)
key to its memory size.key - the name of the class or the Class the mapping to the
memory size shall be removedpublic int size()
public boolean isEmpty()
public void clear()