ASP.NET 2.0 Performance Inspection Questions - Application State

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, Rico Mariani, and Alex Mackman


Do you store STA COM components in application state?

Avoid storing STA COM components in application state where possible. Doing so effectively bottlenecks your application to a single thread of execution when accessing the component. Where possible, avoid using STA COM objects.


Do you use the application state dictionary?

You should use application state dictionary for storing read-only values that can be set at application initialization time and do not change afterward. There are several issues to be aware of when using application state in your code, such as the following:

  • Memory allocated to the storage of application variables is not released unless they are removed or replaced.
  • Application state is not shared across a Web farm or a Web garden — variables stored in application state are global to the particular process in which the application is running. Each application process can have different values.


For a complete list of the pros and cons of using application state, see "Am I Losing My Memory?" on MSDN at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui09172002.asp.

Consider using the following alternatives to application state:

  • Create static properties for the application rather than using the state dictionary. It is more efficient to look up a static property than to access the state dictionary. For example, consider the following code.


  Application["name"] = "App Name";

It is more efficient to use the following code.


  private static String _appName = "App Name";
  public string AppName
  {
    get{return _appName;}
    set{_appName = value;}
  }


  • Use configuration files for storing application configuration information.
  • Consider caching data that is volatile enough that it cannot be stored in application state, but needs updates periodically from a persistent medium, in the Cache object.
  • Use the session store for user-specific information.


You can identify places where your code uses application state by searching for the string "Application."


More Information For more information about the questions and issues raised in this section, see ASP.NET 2.0 Performance Guidelines - Application State.

Personal tools