Extended Weak References
Introduction:
Many applications require a way to persistently store settings and other data. On the full .NET Framework you can use the file system for this. The new 3.0 .NET Micro Framework system adds support for a file system but it’s up to the device manufacturer to add support for this. And this file system isn’t the most efficient way to store simple application data. The .NET Micro Framework has a more efficient way to persistently store information. This are Extended Weak References. In this post I try to explain how to use these Extended Weak References. It isn’t written to be absolutely complete but to get you started.
How C# stores information:
C# is a garbage-collected language. This means you don’t have to allocate and free memory for variables. You just create the variable, C# allocates the memory. Periodically a garbage collector checks which variables aren’t used anymore and frees the memory for them. To understand how the garbage collector know this you have to know what a variable is: A variable is basically just a reference to a memory location. Their only available in their scope (in the current loop, in the current function, etc..). When the application progresses beyond the variable scope the variable isn’t available anymore, so the reference to the memory location goes away. The memory location itself still exists. This is the moment the garbage collector kicks in.. For all allocated memory the garbage collector periodically checks if there is a reference to it, when there are no references it frees the memory location. This sounds very simple but writing a garbage collector is very complicated. So there are some methods to help the garbage collector a bit. One of these methods is the Weak reference.
Variables are strong references. When the garbage collector is freeing memory the variable references hang on for dear life and don’t let go. But imagine you want to store information that you want to store but when running low on memory (which is very real on a resource constraint device) it can be deleted. This is where weak references can be used. Imagine you want to build an iTunes like application. When the user clicks on an artist the device searches for all song for this artist. Searching all files can take a considerable time so when you found the songs you want to store the list. Doing this for every artist and album combination requires a huge amount of memory far more than most .NET Micro Framework devices can provide. This is an ideal case for weak references. When the data is available in memory load that else create it.
Example:
The example creates a new Weak reference. In the function it checks if the reference is still available, when it’s not it recreates the data.
public WeakReference _SongStore = new WeakReference(null);
// Test function
public void TestWeakRefence()
{
// Get arraylist
ArrayList SongStore = (ArrayList) _SongStore.Target;
// Check if data is still available
if (SongStore == null)
{
// If not recreate it
for (Int32 x = 0; x < 100000; x++)
SongStore.Add(x);
// Trigger saving Weak reference data
_SongStore.Target = SongStore;
}
// Now you can use the SongStore ArrayList
}
Why tell me about a less persistent reference type when I need a more persistent data type? Because the work about the same.
Example:
The example creates a new Extended Weak reference. It is set to survive a power cycle. The priority is set tho PriorityLevel.System wich is the highest priority. It checks if the data is still available, when not it recreates it.
public void GetPersistent()
{
// Create or recover our Weak reference
_StorePersistent = ExtendedWeakReference.RecoverOrCreate(
// Type of data
typeof(ArrayList),
// Unique ID for this data,
0,
// The data should be available after power down
ExtendedWeakReference.c_SurvivePowerdown
);
// Here we define the priority, We set the highest priority
_StorePersistent.Priority = (int)ExtendedWeakReference.PriorityLevel.System;
// Retrieve Persistant data
ArrayList PersistantData = (ArrayList)_StorePersistent.Target;
// If Persistant data is not found recreate it
if (PersistantData == null)
{
// If not recreate it
for (Int32 x = 0; x < 100000; x++)
PersistantData.Add(x);
// Trigger save
_StorePersistent.Target = PersistantData;
}
// Do you’re thing..
// When the persistent data must be updated trigger a
// save with _StorePersistent.Target = PersistantData;
}
- MSDN documentation about ExtendedWeakReference class:
http://msdn.microsoft.com/en-us/library/cc561926.aspx - Tutorial about Weak references:
http://blog.paranoidferret.com/index.php/2008/08/13/csharp-tutorial-weak-references/
- Jan Kucera ExtendedWeakReference Example:
http://informatix.miloush.net/microframework/FAQ.aspx?Core%2FExtendedWeakReference
- Expert .NET Framework, book by Jens Kühner
http://informatix.miloush.net/microframework/Go.axd/Amazon-978-1590599730
- ExtendedWeakReference example in .NET MF 3.0 SDK
http://www.microsoft.com/downloads/details.aspx?FamilyID=9356ed6f-f1f0-43ef-b21a-4644dd089b4a&displaylang=en
Recent Comments