Caching with Weak References in .NET

Caching with Weak References in .NET

o cache objects returned from a service, you can use one of several available mechanisms, the simplest of which is to reference the objects indefinitely. However, this approach ultimately suffers if an application caches too much data. Alternatively, you could use the Microsoft Caching Application Block (CAB), an extensible module that facilitates common caching scenarios. But when you’re creating a sophisticated architecture requiring that no more than one copy of a given object (such as a customer record) exist in memory at a given time, the CAB won’t suffice.

One such scenario arises when you’re implementing change notification. To implement change notification in a service-oriented environment, the client needs to have a reference to all client objects to deliver events. When using traditional caching techniques, such as the CAB, there is a delay between the time that the cache releases the reference to an object and the time that object actually gets collected, because the UI is still referencing the formerly cached object. During this time, there is no way for a change notification event to be routed to the object. Also, while the UI is still referencing an object (but the cache is not), the client framework will create another copy of that object the next time the UI asks for it (perhaps in a different screen).

Fortunately, you can approach the problem another way?by using the WeakReference class. According to the MS documentation, “? the WeakReference class cannot be treated as an automatic solution to memory management problems. You still have to establish rules for your application, that is, a caching policy, about what entries are kept or removed from a cache.” However, building a cache manager that uses WeakReference objects is a viable approach.

The solution described in this article uses a combination of a WeakReference and a strong reference to create a cache that will meet the application’s demands.

Getting Started
This solution revolves around the CacheReference class. At the heart of this class are two fields:

   class CacheReference       where T : class   {      private WeakReference m_weakReference;      private T m_strongReference;   ...

Initially, the object you want to cache will be referenced by m_strongReference. This member is strongly typed (using the generic type T), so it performs better than wrapping an object type. You use the m_weakReference member to mark the object as ready for collection. The Target property wraps access to these fields. It takes care of returning the cached value or returns null if the object has been garbage collected:

   public T Target   {      get      {         //Check to see if we have a strong reference         if (m_strongReference != null)            return m_strongReference;         if (m_weakReference == null)            throw new InvalidOperationException("Unable to return                 Target when it has been collected");         if (m_weakReference.IsAlive)            return (T)m_weakReference.Target;         //The target has been collected - ditch the weak reference               m_weakReference = null;         return null;      }      set      {         m_weakReference = null;         m_strongReference = value;      }   }

You need to provide a way to specify that the cached object is eligible for garbage collection. To make the cached object collectable, switch from using the strong reference to the weak reference. That change allows the garbage collector to collect the object (if there are no other references to the object). One additional benefit of the CacheReference class is that an object that has been marked as collectible can be promoted back to a strong reference if it has not yet been garbage collected. This behavior is governed by the AllowCollection property (see Listing 1).

Checking the Pulse
Finally, you need a way to determine whether the item has been collected or not. The IsAlive property returns true if you have a strong reference; otherwise, it returns the value of the m_weakReference.IsAlive property:

   public override Boolean IsAlive   {      get      {         if (m_strongReference != null)            return true;         if (m_weakReference == null)            return false;         return m_weakReference.IsAlive;      }   }

You should check this property before attempting to access the cached object to avoid an InvalidOperationException.

Abstract the Non-Generic Properties
To make caching a list of objects of different types easier, you can move some items to a base class. The following code shows the base class as well as an added field that keeps track of the last time an instance of this object was accessed. The cache manager uses that field value to expire the object:

   abstract class CacheReference   {      protected DateTime m_lastAccess;               public CacheReference()      {         m_lastAccess = DateTime.Now;      }         public DateTime LastAccess      {         get { return m_lastAccess; }      }         public abstract Boolean IsAlive { get; }      public abstract Boolean AllowCollection { get; set; }   }

Exploring the CacheManager Class
This class is similar to the CacheManager class that is part of the Caching Application Block, but this version is a little less pluggable because it contains a hard-coded scavenging scheme:

   class CacheManager   {      private Dictionary<String, CacheReference> m_cache = new                   Dictionary<string, CacheReference>();

The m_cache field is a dictionary of CacheReference objects that will contain all the cached items. You can add CacheReference objects because CacheReference inherits from the non-generic CacheReference class.

You will need a method to add an item to the cache. Use a generic method so that you can create a CacheReference instance to hold the item.

   public void Add<T>(String id, T tocache) where T : class   {      m_cache[id] = new CacheReference<T>(tocache);   }

You’ll also need a way to get items back out. The Get method shown below handles item retrieval. You pass in a string ID; the Get method takes care of checking to see if the provided ID exists in the cache. If so, the method verifies that the object is alive. If the item has already been collected (when temp.IsAlive == false), the method removes the item from the cache:

   public T Get<T>(String id) where T : class   {      CacheReference temp = null;         if (m_cache.TryGetValue(id, out temp))      {         if (temp.IsAlive)         {            //The object is alive and doing well - return it.            return ((CacheReference<T>)temp).Target;         }         else         {            //The item has been garbage collected.  Remove it.            m_cache.Remove(id);               return null;         }      }      else          return  null;   }

Note that the Get method calls a method named Remove, which removes objects from the cache. Remove is a separate method because you’ll also want to be able to explicitly remove an item from the cache:

   public void Remove(String id)   {      CacheReference cacheReference;         if (m_cache.TryGetValue(id, out cacheReference))      {         if (cacheReference.IsAlive)            cacheReference.AllowCollection = true;         m_cache.Remove(id);      }   }

So far, you have only basic cache functionality: adding, retrieving, and removing items.

Taking Out the Garbage
The last piece of the puzzle is to provide an expiration mechanism. A straightforward mechanism to expire objects is to scavenge x number of objects after y objects have been added to the cache. To facilitate this approach, add some code to the CacheManager class that gives you the ability to set those values externally:

   private int m_maxItemsBeforeScavenge;   private int m_itemsToScavenge;      public CacheManager(int maxItemsBeforeScavenge, int itemsToScavenge)   {      m_maxItemsBeforeScavenge = maxItemsBeforeScavenge;      m_itemsToScavenge = itemsToScavenge;   }

A simple scavenging scheme (see Listing 2) attempts to expire a number of objects (m_itemsToScavenge) whenever the cache grows to a predetermined number of objects (m_maxItemsBeforeScavenge). Note that the Scavenge method uses an explicit call to GC.Collect. While calling GC.Collect explicitly is normally frowned upon, calling it after dereferencing an unusually large number of objects is acceptable (and desirable in this case).

Putting the Cache into Action
The downloadable sample application (see Figure 1) lets you test the cache, using a very small Foo test class:

   class Foo   {      public String Name;   }
Author’s Note: In production, this class wouldn’t be a good candidate for caching using weak references because the WeakReference itself could consume more memory than the object that it’s referencing.

?
Figure 1. Testing the Cache: The sample application creates and caches a large number of objects and lets you see how many have been garbage collected.

This is the code that generates the objects:

   for (int x = 0; x < 50000; x++)   {      Foo foo = new Foo();      foo.Name = "Bar " + x.ToString();         if (x == 0)      m_foo = foo;         m_cacheManager.Add<Foo>(foo.Name, foo);   }

Here's the output:

   Scavenging is starting.  There are 40001 items in the list.   Scavenging is Complete.  There are 39503 items in the list.   Scavenging is starting.  There are 40001 items in the list.   Scavenging is Complete.  There are 39502 items in the list.   Scavenging is starting.  There are 40001 items in the list.   Scavenging is Complete.  There are 39502 items in the list.   Scavenging is starting.  There are 40001 items in the list.   Scavenging is Complete.  There are 39502 items in the list.

The WeakReference object is very powerful, but?just as Microsoft warns?you must combine it with additional logic to make it useful in a caching application. But you'll find the effort required to take advantage of the CacheReference class worthwhile if you need to distribute events from a service to client objects.

devx-admin

devx-admin

Share the Post:
USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted