0

I define static variable in Global.asax.And I want to use that variable in all web page of my site.

So I just want to know is there any disadvantages of that global static variable with respect to memory and performance in web application?

Is there any specific advantage of application varibale over global static variable with respect to memory and performance.

Touseef
  • 414
  • 1
  • 5
  • 17
  • 1
    Maybe this would be of some interest http://stackoverflow.com/questions/894036/what-is-better-static-variable-v-s-asp-net-application-session and it actually depends on what is the purpose you need to serve – V4Vendetta Jul 22 '11 at 11:32

1 Answers1

3

Assuming your are trying to cache a simple value, there's no real disadvantage memory or performance wise but it depends on what you are trying to do.

If you need a handy place to keep read-only value known at compilation, it's probably better to use a const.

If you want to cache some simple global value, like the application version number as a string, it's perfectly ok to put that in a static.

One thing you should realize is that the Application object (ie. Global.asax) is not a singleton. There could be more than one instance of the application, for example when IIS decides it's time to recycle the app pool. AFAIK the application instances will run in different AppDomains so there will also be multiple instances of your static variable.

So, you should never use a static variable on the application object to store information modified at runtime. There is simply no guarantee that the information is persisted across requests.

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74