Welcome to YLOAN.COM
yloan.com » Computers & Internet » Silverlight Memory Leak While Using WCF RIA Services
Games Personal-Tech Data Entry registry cruise torrent mac code virus storage uninstaller systems cisco bugs wireless codes maintenance dell update communication trojan atlanta Data Backup Data Storage Data Protection Data Recovery Anti-Virus Windows Linux Software Hardware Mobil-Computing Certification-Tests Computers & Internet Internet

Silverlight Memory Leak While Using WCF RIA Services

Silverlight Memory Leak While Using WCF RIA Services


When we want to load data through RIA services, the code we usaully write looks loke bellow:

_context =new ProductContext();

_context.Load(_context.GetProductsQuery(), d=>


{

this.Products=new ObservableCollection(d.Entities);

},null);

But the above code will cause a great increase of memory usage, enven through you call GC.Collect() after the load operation completes.

Solution

The solution for this problem is as simple as the following code shows:

private void LoadProducts()

{

_context =new ProductContext();

_context.Load(_context.GetProductsQuery(),LoadBehavior.MergeIntoCurrent,LoadProductsCompleted,null);

}

private void LoadProductsCompleted(LoadOperation loadOperation)

{

if (this.Products ==null)

{

this.Products =new Obs

ervableCollection(loadOperation.Entities);

}

}

As you have noticed that the key point is to useLoadBehavior. There are 3 values of LoadBehavior:

LoadBehavior.KeepCurrent: The cached instance is not changed and the newly read instance is discarded. If an instance's key property (marked as KeyAttribute) doesn't change, this instance will not be updated.This is a very important feature!

LoadBehavior.MergeIntoCurrent: Values from the newly read instance are merged into the cached instance for property values that are unmodified. No changes are lost in this merge.

LoadBehavior.RefreshCurrent: All members of the cached instance are overwritten with current values from the newly read instance,regardless of whether they have been modified. In addition, the original state of the entity is also set to the newly read instance.

You can use the right load behavior to extremly improve your app's performance.

Research report

About the former problem and solution, I have done a test to show how great the solution can save memory usage. In the test, the ViewModel loads 2500 products every 5 seconds, and each products' IDs do not change (range from 1 to 2500). First, let's see how the memory usage increases when using the old context.Load() method: The following picture shows the memory usages when the new context.Load() method is applied.

Conclusion

All the entities loaded to the client side are cached in Silverlight.


Everytime when you new an instance, the instance will be cached, and it will make memory usage increase. I don't know if we can clear the cache or dispose the instance manually, but till now, I haven't found the way.

If you use frames in your Silverlight applications, please set the pages'NavigationCacheMode property to "Enabled", since all the pages are actually cached, even if you set the property to "Disabled".

This section will help you better understand the memory leak in Silverlight 4.

Source:http://www.novasoftware.com/
System Monitoring Software Programs For Companies For Laptops and Netbooks – There is a Big Market After All How to Speed Up a PC and Boost Your Computer's Performance in Just a Few Minutes Ping Could Be Draining iPhone Battery Life How to Speed Slow PC Up?Steps Helping You Fix a Slow Computer Immediately! Ways to Speed Up Your Computer! How To Watch Premium TV On Computer - Watching Premium TV On A Personal Computer Four Tips to Speed up Slow Computers The Best Way To Recharge The Batteries Of Cell Phone Tips about Tips on how to Strengthen Long term Memory Diet Programs And Menus - The Shelton Diet Use of Computer in Disc Jockeying How Does Advair Diskus Help Asthma Patients
print
www.yloan.com guest:  register | login | search IP(3.143.214.100) / Processed in 0.008141 second(s), 5 queries , Gzip enabled , discuz 5.5 through PHP 8.3.9 , debug code: 70 , 3076, 55,
Silverlight Memory Leak While Using WCF RIA Services