How to: Configure Garbage Collection

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

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

There are two possible ways to configure the cache reference type behavior (garbage collection) - by using the Model Settings Editor or by using the BackendConfiguration class in the code.

Using the Model Settings Editor

In order to configure the cache reference type behavior by using the Model Settings Editor:

  1. Open your domain model in the Visual Designer.
  2. Right-click on an empty area and select Show Model Settings.
  3. In the Model Settings Dialog navigate to the Backend Settings -> Runtime Configuration tab page.
  4. Modify the Cache Reference Type option.

Using the BackendConfiguration Class

The second way for configuring the cache reference type is to use the BackendConfiguration class. To do this use the CustomizeBackendConfiguration partial method by implementing it in a separate code file for your partial context class. Modify the cache reference type in the following manner:

public partial class EntitiesModel
{
    static partial void CustomizeBackendConfiguration(ref BackendConfiguration config)
    {
        config.Runtime.CacheReferenceType = CacheReferenceType.Weak;
    }
}
Partial Public Class EntitiesModel

    Private Shared Sub CustomizeBackendConfiguration(ByRef config As BackendConfiguration)
        config.Runtime.CacheReferenceType = CacheReferenceType.Weak
    End Sub

End Class