Thursday, April 28, 2016

How to accelerate XAF application


XAF applications use the Application Model a lot. When the quantity of modules in the app grows, the Application Model becomes more complicated (because of the increasing number of layers in the Application Model). It slows the Application Model performance down and leads to notable time lags in the application itself. The growing amount of time needed for the app to start is the most obvious sign of such changes.

To solve the issue mentioned above, use the Xafari.Accelerator module designed precisely to fix the problems with the Application Model performance.

Effects of Usage

Being used in demo applications, Xafari.Accelerator showed twice as less starting time compared to the same apps without this module. The actual result for a particular project depends on the complexity of the application and the quantity of business objects in use.
The results of demo application profiling proved that the usage of Xafari.Accelerator allows excluding all operations with the Application Model from the profiling reports. When Xafari.Accelerator is not used, the operations with the Application Model are critical according to the application profiling reports.

Profiling EAM application

I profile application Galaktika.EAM and colect results of usage Xafari.Accelerator. I marked the time that has passed since the beginning of the start of the application:

StagesStandard start, сAccelerator using, сAcceleration, times
Splash screen450.8
Startup View63331.9

How to Start

Module Xafari.Accelerator is included to Xafari x08 and above.

Module Adding

To start using the module, add the overloaded methods to the descendant class as shown in two code samples below.

For WinApplication:

using System.Collections.Generic;
using Xafari;
using Xafari.Win;

public class NorthwindWindowsFormsApplication : WinApplication
 protected override ApplicationModelManager CreateModelManager(IEnumerable<Type> boModelTypes)
  return this.Xafari().Win().CreateModelManager(boModelTypes, base.CreateModelManager);

 protected override void OnSetupStarted()

For WebApplication:

using System.Collections.Generic;
using Xafari;
using Xafari.Web;

public class NorthwindAspNetApplication : WebApplication
 protected override ApplicationModelManager CreateModelManager(IEnumerable<Type> boModelTypes)
  return this.Xafari().Web().CreateModelManager(boModelTypes, base.CreateModelManager);

 protected override void OnSetupStarted()
These methods add the necessary extension points that are further used by the accelerator. If Xafari.Accelerator is disabled or not used, the behavior of such methods remains the same. Such approach protects the application from any negative effects that newly implemented methods can cause.
Now the Xafari.Accelerator module can be added to the application in VS through the Application Model designer. It is also possible to add it other ways, e.g. via the .config file of the application.
Xafari.Application requires the application to run in .NET 4.5.1+ mode.

Module Usage

Accelerator Start

By default, Xafari.Accelerator is available but not used. To start it, the administrator of the application should use the Start Accelerator option in the Tools tab.

If the operation was successful, the following popup window is shown:

Xafari.Accelerator will be in use as soon as the application is launched the next time.

Accelerator Stoppage

The administrator of the application can use the Stop Accelerator option in the Tools tab to disable Xafari.Accelerator.

If the operation was successful, the following popup window is shown:

The Accelerator will be disabled during the next launch of the application.
The alternative way to disable the accelerator is to remove its files ApplicationName.Accelerator.xafml (ApplicationName.Accelerator_locale.xafml) from the same folder that stores the ModelAssembly.dll file (by default, it should be the application folder).

Temporary Accelerator Stoppage

When it is needed to turn the accelerator off for a particular period only, use the following key in the configuration file:

  <add key="Xafari.Accelerator" value="False" />
Alternatively, the accelerator can be temporarily disabled in the code as shown below:

XafariAcceleratorModule.Enabled = false

How It Works

Xafari.Accelerator core principle is the usage of a separate XML file to store the whole Application Model unchangeable part for the current combination of modules and their versions. The same idea works for localization files if there are any.
The accelerator files ApplicationName.Accelerator.xafml (ApplicationName.Accelerator_locale.xafml) are stored in the same folder that has the ModelAssembly.dll file. By default, it is the application folder.
The Accelerator then modifies the Application Model so that its unchangeable part builds upon this only file.
As a result, there are only two layers left in the Application Model: the accelerator and the user.
The Xafari.Accelerator module also extends the Application Model with extra interfaces to avoid performance issues that may happen when adding CustomMembers.

Required assemblies

  • Xafari.Utils.dll
  • Xafari.dll
  • Xafari.Win.dll (Xafari.Web.dll)
  • Xafari.Web.dll
  • Xafari.Web.Utils.dll)

Tuesday, April 12, 2016

How to quick find memory leaks on using IObjectSpace

In my practice, memory leaks in XAF application in 90% of cases occur by using undisposed IObjectSpace.

As is known in the XAF application all data operations are made through the session and IObjectSpace.
If you do not call the Dispose() method a large amount of memory will not be released. And after some time of using such a code your application grabs a huge amount of memory and this will adversely affect the performance and reliability of application.
Search for memory leaks is time consuming and requires highly skilled developer.

There is a standard approach to deal with such memory leaks by C# using keywords.

using (var objectSpace = Application.CreateObjectSpace())

But in some cases the use of the using keyword is impossible. And fix memory leaks becomes very complex task.

To simplify this task Xafari Framework provides to developers a standard solution.

Xafari implements special Controller and Action to inspect all created and not released instances of UnitOfWorks and IObjectSpace. See picture below how to run report of active instances.

Show Memory Leaks report

All active instances of UnitOfWork and IObjectSpace collected into 2 lists (see on picture). You can see information about Owner, Age and Time of creation.

IObjectSpace Memory Leaks report

Detail information additionally contains the stack trace and serves to quick find source code line to fix memory leaks.

Detail info for IObjectSpace

I hope this article is usefull for you.

Hello all XAF developers

Hi everyone
My name is Sergey Zaitsev. I am a leading developer of Xafari Framework.
This blog will discuss a variety of questions about the use DevExpress XAF and Xafari Framework.
We show some examples of how to use Xafari and XAF components, and answer your questions.