Lifetime of ASP.NET Static Variable

I am holding some information in static variables defined in page class (not in Global.asax). I only declare variable in code like:

protected static int SomeGlobalUnsecureID;
protected static string SomeGlobalUnsecureString;

and define the variable in PageLoad event. For example, I check ID from the database, if it's different from SomeGlobalUnsecureID, I update SomeGlobalUnsecureID and String from somewhere else, otherwise leave them as is. This is perfectly safe in my app. logic (i.e. those data are not secure, everybody can access them, no problem); only thing I want to accomplish is

  1. Hold the same amount of memory regardless of users connected

  2. Change if and only if persistent info is different from the one in 'memory' (because actually reading the string is time consuming for me.

Now, since I make the check in PageLoad, I have no problems in reloaded pages. However my page is full of WebMethods, and sometimes I see that the static variables are zeroed. And the strange part is; the session is still active even when the static variables are zeroed (so-> no server or app. pool restart etc.)

This is really strange for me. I assume that static variable will hold its value until the application (somehow) ends. But even the Session did not expire, the static variable is zeroed. What do you suggest? Is using application variables a better choice? All documents I've read on web suggest static variables instead of application variables, do I need to declare them somehow different?


Solution 1:

Static variables persist for the life of the app domain. So the two things that will cause your static variables to 'reset' is an app domain restart or the use of a new class. In your case with static variables stored in an aspx Page class, you may be losing the static variables when ASP.NET decides to recompile the aspx Page into a new class, replacing the old page class with the new one.

For those reasons if the system decide to restart or replace the class (.NET doesn't kill or unload classes/assemblies in a running app domain) then your static variables will reset because you are getting a new class with the restart or replacement. This applies to both aspx Pages and classes in the App_Code folder

ASP.NET will replace a class if for any reason thinks that need to recompile it (see ASP.NET dynamic compilation).

You can't prevent the loss of static variables from an app domain restart, but you can try to avoid it from class replacement. You could put your static variables in a class that is not an aspx page and is not in the App_Code directory. You might want to place them on a static class somewhere in your program.

public static class GlobalVariables
{
    public static int SomeGlobalUnsecureID;
    public  static string SomeGlobalUnsecureString;
}

The static variables are per pool, that is means that if you have 2 pools that runs your asp.net site, you have 2 different static variables. (Web garden mode)

The static variables are lost if the system restarts your asp.net application with one of this way.

  1. the pool decide that need to make a recompile.
  2. You open the app_offline.htm file
  3. You make manual restart of the pool
  4. The pool is reach some limits that you have define and make restart.
  5. For any reason you restart the iis, or the pool.

This static variables are not thread safe, and you need to use the lock keyword especial if you access them from different threads.

Since an app restart will reset your statics no matter what, if you really want to persist your data, you should store the data in a database using custom classes. You can store information per-user in Session State with a database session state mode. ASP.NET Application State/Variables will not help you because they are stored in memory, not the database, so they are lost on app domain restart too.

Solution 2:

I think the following two points are also important for the lifetime of static variables:

1 - In your application pool's advanced settings check "Recycling" -> "Regular Time Interval (minutes)" setting. It's default value is 1740 which means in each 29 hours your static variables are lost because of recycling of your application pool. This setting is used for termination of possible memory leaks. I would not change this setting..

2 - In your application pool's advanced settings check "Process Model" -> "Idle Time-out (minutes)" setting. It's default value is 20 which means in each 20 minutes of inactivity in your application pool, the worker processes are terminated/suspended which will cause your static variables to be lost. This setting is used for freeing up resources when the application pool is not used in some amount of time. You can set it to 0 to disable the timeout.