NET 静态变量的生存期

我在页面类中定义的静态变量中保存了一些信息(Global.asax 中没有)。我只在以下代码中声明变量:

protected static int SomeGlobalUnsecureID;
protected static string SomeGlobalUnsecureString;

并在 PageLoad 事件中定义变量。例如,我从数据库中检查 ID,如果它与 Some GlobalUnsecureID 不同,我就从其他地方更新 Some GlobalUnsecureID 和 String,否则就让它们保持原样。这在我的应用程序里是绝对安全的。逻辑(即这些数据不安全,每个人都可以访问它们,没有问题) ; 我唯一想完成的事情是

  1. 无论用户是否连接,都保持相同的内存量

  2. 当且仅当持久信息与 “内存”(因为实际读取字符串是非常耗时的 我

现在,因为我在 PageLoad 中进行了检查,所以在重新加载页面时没有任何问题。然而,我的页面充满了 WebMethod,有时我看到静态变量被归零。奇怪的是,即使静态变量为零(所以-> 没有服务器或应用程序) ,会话仍然是活动的。水池重新启动等)

这对我来说真的很奇怪。我假设静态变量将保持它的值,直到应用程序(以某种方式)结束。但是即使 Session 没有过期,静态变量也被归零。你有什么建议?使用应用程序变量是更好的选择吗?我在网上看到的所有文档都建议使用静态变量而不是应用程序变量,我是否需要声明它们有所不同?

101687 次浏览

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.

Static variable is used to store the all the object for same value

protected void Page_Load(object sender, EventArgs e)
{
sss s1, s2;
s1 = new sss();
s1.TotalMark = 10;
s2 = new sss();
s2.TotalMark = 20;
sss.SchoolName = "St.Joseph's Hr.Sec.S"; //We can access through class and assign common to all
s1.PrintData();
s2.PrintData();
}


public class sss
{
public static string SchoolName { set; get; }
public int TotalMark { set; get; }
public string StudentName{set;get;}
public void PrintData()
{
Console.WriteLine(TotalMark);
Console.WriteLine(SchoolName);
Console.WriteLine(StudentName);
}
}

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.