6

I was just wondering this the other day. I am not exactly sure how ASPX manages the garbage disposal, but as far as I can tell the "finished loading" does not remove static memory values or after the page has been reloaded. Static at least in terms of C means that the memory allocation follows your program until the program itself is shut down. Is this the same way in ASPX? If I have a static value and I go from Page A to Page B, is that static value still persistent in the RAM until they leave the application or is that value removed once I am no longer on Page A? (go to a different website removing their instance off the application pool in the server).

From what I have experienced:

  public static class foo
  {
      public static int x;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
      foo.x++; //This will continue to increment from the last value before reload
  }
4

2 回答 2

5

在 ASP.NET 中应避免使用静态类。它们一直保留在内存中,直到应用程序重新启动,并且受到许多并发错误和竞争条件的影响。

并且关闭用户会话(浏览器会话)不会重新启动应用程序!即使用户离开并回来,它们也会保留在内存中。所以真的真的避免静态类!

于 2012-06-20T22:00:22.903 回答
0

这是您的标准 CLR 执行模型,对于 asp.net 没有什么不同。静态对象被视为应用程序的根,不会被垃圾收集。

这是一篇关于垃圾收集如何在 .net 中工作的旧文章,但我认为所有原则仍然相同:http: //msdn.microsoft.com/en-us/magazine/bb985010.aspx

于 2012-06-20T21:59:34.827 回答