Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Type and Size

< EclipseLink‎ | UserGuide‎ | JPA‎ | Basic JPA Development‎ | Caching
Revision as of 13:46, 10 May 2012 by James.sutherland.oracle.com (Talk | contribs) (NONE and CACHE)

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


Cache Type and Size

EclipseLink provides several different cache types which have different memory requirements. The size of the cache (in number of cached objects) can also be configured. The cache type and size to use depends on the application, the possibility of stale data, the amount of memory available in the JVM and on the machine, the garbage collection cost, and the amount of data in the database.

By default EclipseLink uses a SOFT_CACHE with an initial size of 100 objects. The cache size is not fixed, but just the initial size, EclipseLink will never eject an object from the cache until it has garbage collected from memory (unless the CACHE type is used, but this is not recommended). The cache size of the SOFT_CACHE and HARD_CACHE is also the size of the soft or hard sub-cache that can determine a minimum number of objects to hold in memory.


Cache Options

Option (cache type) Memory Use

FULL

Very High

WEAK

Low

SOFT

High

SOFT_CACHE and HARD_CACHE

Medium

There are two other options, NONE, and CACHE. These options are not recommend.

For more information, see Guidelines for Configuring the Cache Type.

FULL

This option provides full caching: objects are never flushed from memory unless they are deleted.

It caches all objects and does not remove them even if memory is low. Cache size doubles whenever the maximum size is reached. This method may be memory-intensive when many objects are read. Do not use this option for entities with a large number of instances.

We recommend using FULL only when the data set size is small and memory is in large supply.

WEAK

This option only caches objects that have not been garbage collected. Any object still referenced by the application will still be cached.

The weak cache uses less memory than the full cache but also does not provide a durable caching strategy across client/server transactions. Objects are available for garbage collection when the application no longer references them on the server side (that is, from within the server JVM).

SOFT

This option is similar to the weak cache, except that the cache uses soft references instead of weak references. Any object still referenced by the application will still be cached, and objects will only be removed from the cache when memory is low.

The soft cache allows for full caching of the objects, while still allowing the JVM to garbage collect the objects if memory is low.

SOFT_CACHE and HARD_CACHE

These options are similar to the weak cache except that they maintain a most frequently used sub-cache. The sub-cache uses soft or hard references to ensure that these objects are not garbage collected, or only garbage collected only if the JVM is low on memory.

The soft cache and hard cache provide more efficient memory use. They release objects as they are garbage-collected, except for a fixed number of most recently used objects. Note that weakly cached objects might be flushed if the transaction spans multiple client/server invocations. The size of the sub-cache is proportional to the size of the cache as specified by the size option. You should set the cache size to number of objects you wish to hold in your transaction.

We recommend using this cache in most circumstances as a means to control memory used by the cache.

For more information, see What you may need to Know About the Internals of Weak, Soft, and Hard Caches.

NONE and CACHE

NONE and CACHE options do not preserve object identity and should only be used in very specific circumstances. NONE does not cache any objects. CACHE only caches a fixed number of objects in an LRU fashion. These cache types should only be used if there are no relationships to the objects.

We do not recommend using these options. To disable caching set the cache isolation to ISOLATED instead.

Cache Type Examples

@Cache type annotation example
...
@Entity
@Cache(
  type=CacheType.SOFT,
  size=10000
)
public class Employee {
  ...
}
Cache type XML example
<?xml version="1.0"?>
<entity-mappings
	xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_4.xsd"
	version="2.4">
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
        <cache type="SOFT" size="10000"/>
    </entity>
</entity-mappings>

Guidelines for Configuring the Cache Type

You can configure the cache at the project or descriptor level.

Use the following guidelines when configuring your cache and identity map:

  • If objects with a long life span and object identity are important, use a SoftIdentityMap, SoftCacheWeakIdentityMap or HardCacheWeakIdentityMap policy. For more information on when to choose one or the other, see What you may need to Know About the Internals of Weak, Soft, and Hard Identity Maps.
  • If object identity is important, but caching is not, use a WeakIdentityMap policy.
  • If an object has a long life span or requires frequent access, or object identity is important, use a FullIdentityMap policy.

Elug warning icon.png

Warning: Use the FullIdentityMap only if the class has a small number of finite instances. Otherwise, a memory leak will occur.

  • If an object has a short life span or requires frequent access, and identity is not important, use a CacheIdentityMap policy.
  • If objects are discarded immediately after being read from the database, such as in a batch operation, use a NoIdentityMap policy. The NoIdentityMap does not preserve object identity.

Elug note icon.png

Note: We do not recommend the use of CacheIdentityMap and NoIdentityMap policies.

What You May Need to Know About the Internals of Weak, Soft, and Hard Cache Types

The WeakIdentiyMap and SoftIdentityMap use JVM weak and soft references to ensure that any object referenced by the application is held in the cache. Once the application releases its' reference to the object, the JVM is free to garbage collection the objects. When a weak and soft reference is garbage collected - is determined by the JVM. In general one could expect a weak reference to be garbage collected on each JVM garbage collector, and a soft reference to be garbage collected when the JVM determines memory is low.

The SoftCacheWeakIdentityMap and HardCacheWeakIdentityMap types of identity map contain the following two caches:

  • Reference cache: implemented as a LinkedList that contains soft or hard references, respectively.
  • Weak cache: implemented as a HashMap that contains weak references.

When you create a SoftCacheWeakIdentityMap or HardCacheWeakIdentityMap with a specified size, the reference cache LinkedList is exactly this size. The weak cache HashMap is initialized to 100 percent of the specified size: the weak cache will grow when more objects than the specified size are read in. Because EclipseLink does not control garbage collection, the JVM can reap the weakly held objects whenever it sees fit.

Because the reference cache is implemented as a LinkedList, new objects are added to the end of the list. Because of this, it is by nature a least recently used (LRU) cache: fixed size, object at the top of the list is deleted, provided the maximum size has been reached.

The SoftCacheWeakIdentityMap and HardCacheWeakIdentityMap are essentially the same type of identity map. The HardCacheWeakIdentityMap was constructed to work around an issue with some JVMs.

If your application reaches a low system memory condition frequently enough, or if your platform's JVM treats weak and soft references the same, the objects in the reference cache may be garbage-collected so often that you will not benefit from the performance improvement provided by it. If this is the case, we recommend that you use the HardCacheWeakIdentityMap. It is identical to the SoftCacheWeakIdentityMap except that it uses hard references in the reference cache. This guarantees that your application will benefit from the performance improvement provided by it.

When an object in a HardCacheWeakIdentityMap or SoftCacheWeakIdentityMap is pushed out of the reference cache, it gets put in the weak cache. Although it is still cached, EclipseLink cannot guarantee that it will be there for any length of time because the JVM can decide to garbage-collect weak references at anytime.


Eclipselink-logo.gif
Version: 2.4 DRAFT
Other versions...

Back to the top