How to: Configure Garbage Collection

Garbage Collection is an integral part of the .NET Framework world. The .NET's garbage collector (GC) manages the allocation and release of memory for your applications. Garbage collection in Telerik Data Access is extremely important because of the number of potential references in the first level cache.

Telerik Data Access allows you to configure the type of the references maintained by the cache of the OpenAccessContext. There are three possible modes:

  1. Strong - specifies that all references can't be reclaimed.
  2. Weak - specifies that a reference is maintained using a System.WeakReference allowing the garbage collector to reclaim the object.
  3. Auto - specifies that references to fetched data are strongly held and references to user instances are weakly held. When data is fetched from the server (or the second level cache), it is put into state objects. These state objects are then put (or updated in) the first level cache. The first level cache will reference the state instances with a string reference, until the persistent object (that the state holds the values for) is generated. Then the first level cache strong reference is updated to a weak reference as the user application is now responsible to hold the reference to the object. In this way, fetched data is not lost due to GC activities, but the data can be eventually garbage collected when the application has lost the reference to that data.

The default cache reference type behavior is Auto.

Configuring Cache Reference Type Behavior

In order to configure the cache reference type you can use the instance of the BackendConfiguration class, which is passed to the constructor of the context every time you call it:

public static BackendConfiguration GetBackendConfiguration()
    BackendConfiguration backend = new BackendConfiguration();
    backend.Backend = "MsSql";
    backend.ProviderName = "System.Data.SqlClient";

    backend.Runtime.CacheReferenceType = CacheReferenceType.Weak;

    CustomizeBackendConfiguration(ref backend);

    return backend;
Public Shared Function GetBackendConfiguration() As BackendConfiguration
    Dim backend As BackendConfiguration = New BackendConfiguration()
    backend.Backend = "MsSql"
    backend.ProviderName = "System.Data.SqlClient"

    backend.Runtime.CacheReferenceType = CacheReferenceType.Weak;


    Return backend
End Function