Top Description Inners Fields Constructors Methods
org.hibernate.util

public Class SoftLimitMRUCache

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, .IOException, java.lang.ref.ReferenceQueue, .SoftReference

Cache following a "Most Recently Used" (MRU) algorithm for maintaining a bounded in-memory size; the "Least Recently Used" (LRU) entry is the first available for removal from the cache.

This implementation uses a "soft limit" to the in-memory size of the cache, meaning that all cache entries are kept within a completely java.lang.ref.SoftReference-based map with the most recently utilized entries additionally kept in a hard-reference manner to prevent those cache entries soft references from becoming enqueued by the garbage collector. Thus the actual size of this cache impl can actually grow beyond the stated max size bound as long as GC is not actively seeking soft references for enqueuement.

The soft-size is bounded and configurable. This allows controlling memory usage which can grow out of control under some circumstances, especially when very large heaps are in use. Although memory usage per se should not be a problem with soft references, which are cleared when necessary, this can trigger extremely slow stop-the-world GC pauses when nearing full heap usage, even with CMS concurrent GC (i.e. concurrent mode failure). This is most evident when ad-hoc HQL queries are produced by the application, leading to poor soft-cache hit ratios. This can also occur with heavy use of SQL IN clauses, which will generate multiples SQL queries (even if parameterized), one for each collection/array size passed to the IN clause. Many slightly different queries will eventually fill the heap and trigger a full GC to reclaim space, leading to unacceptable pauses in some cases.

Note

This class is serializable, however all entries are discarded on serialization.

Authors
Steve Ebersole, Manuel Dominguez Sarmiento
See Also
org.hibernate.cfg.Environment#QUERY_PLAN_CACHE_MAX_STRONG_REFERENCES, org.hibernate.cfg.Environment#QUERY_PLAN_CACHE_MAX_SOFT_REFERENCES

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_SOFT_REF_COUNT

The default soft reference count.

public static final int
DEFAULT_STRONG_REF_COUNT

The default strong reference count.

private transient ReferenceQueue<T>
private transient LRUMap
private final int
private transient LRUMap
private final int

Constructor Summary

AccessConstructor and Description
public
SoftLimitMRUCache()

Constructs a cache with the default settings.

public
SoftLimitMRUCache(int
the strong reference count.
strongRefCount
,
int
the soft reference count.
softRefCount
)

Constructs a cache with the specified settings.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
clear()

Clears the cache.

private void
public synchronized Object

Returns:

the stored value, or null if no entry exists.
get
(Object
the cache key.
key
)

Gets an object from the cache.

private void
init()

public synchronized Object

Returns:

the previous value stored in the cache, if any.
put
(Object
the key.
key
,
Object
the value.
value
)

Puts a value in the cache.

private void
public synchronized int

Returns:

the strong reference cache size.
size
()

Gets the strong reference cache size.

public synchronized int

Returns:

the soft reference cache size.
softSize
()

Gets the soft reference cache size.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_SOFT_REF_COUNTback to summary
public static final int DEFAULT_SOFT_REF_COUNT

The default soft reference count.

DEFAULT_STRONG_REF_COUNTback to summary
public static final int DEFAULT_STRONG_REF_COUNT

The default strong reference count.

referenceQueueback to summary
private transient ReferenceQueue<T> referenceQueue
softRefCacheback to summary
private transient LRUMap softRefCache
softRefCountback to summary
private final int softRefCount
strongRefCacheback to summary
private transient LRUMap strongRefCache
strongRefCountback to summary
private final int strongRefCount

Constructor Detail

SoftLimitMRUCacheback to summary
public SoftLimitMRUCache()

Constructs a cache with the default settings.

See Also
DEFAULT_STRONG_REF_COUNT, DEFAULT_SOFT_REF_COUNT
SoftLimitMRUCacheback to summary
public SoftLimitMRUCache(int strongRefCount, int softRefCount)

Constructs a cache with the specified settings.

Parameters
strongRefCount:int

the strong reference count.

softRefCount:int

the soft reference count.

Exceptions
IllegalArgumentException:
if either of the arguments is less than one, or if the strong reference count is higher than the soft reference count.

Method Detail

clearback to summary
public synchronized void clear()

Clears the cache.

clearObsoleteReferencesback to summary
private void clearObsoleteReferences()
getback to summary
public synchronized Object get(Object key)

Gets an object from the cache.

Parameters
key:Object

the cache key.

Returns:Object

the stored value, or null if no entry exists.

initback to summary
private void init()
putback to summary
public synchronized Object put(Object key, Object value)

Puts a value in the cache.

Parameters
key:Object

the key.

value:Object

the value.

Returns:Object

the previous value stored in the cache, if any.

readObjectback to summary
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
sizeback to summary
public synchronized int size()

Gets the strong reference cache size.

Returns:int

the strong reference cache size.

softSizeback to summary
public synchronized int softSize()

Gets the soft reference cache size.

Returns:int

the soft reference cache size.

org.hibernate.util back to summary

private Class SoftLimitMRUCache.KeyedSoftReference

extends SoftReference
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Object

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private Object
Inherited from java.lang.ref.SoftReference:
get

Field Detail

keyback to summary
private final Object key

Constructor Detail

KeyedSoftReferenceback to summary
private KeyedSoftReference(Object key, Object value, ReferenceQueue<T> q)
Annotations
@SuppressWarnings:unchecked

Method Detail

getKeyback to summary
private Object getKey()