在ASP.NET静态对象的内存浪费? [英] Static objects in ASP.NET a waste of memory?

查看:117
本文介绍了在ASP.NET静态对象的内存浪费?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道这个的一天。我并不完全确定ASPX如何管理垃圾处理,但据我可以告诉加载完成不会删除静态内存值或页面后已重新加载。静态至少在C而言意味着内存分配遵循你的程序,直到程序本身被关闭。这是在ASPX以同样的方式?如果我有一个静态值,我从网页A到网页B,是一个静态值仍然在RAM中的持久直到他们离开应用程序或被删除,一旦我在页面A不再是价值? (去不同的网站移除它们的实例关闭服务器的应用程序池)。

从我所经历的:

 公共静态类Foo
  {
      公共静态INT X;
  }  保护无效的Page_Load(对象发件人,EventArgs的发送)
  {
      foo.x ++; //这将继续从重装前的最后一个值递增
  }


解决方案

静态类应避免在ASP.NET中。他们留在内存中,直到应用程序重新启动,并受到众多并发错误和比赛条件。

和关闭用户会话(浏览器会话)不会重新启动应用程序!他们留在记忆即使用户离开和回来。所以,真的避免静态类!

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
  }

解决方案

Static classes should be avoided in ASP.NET. They stay in memory until the application is restarted and are subject to many concurrency errors and race conditions.

And closing a user session (browser session) does not restart the application ! They stay in memory even if a user leaves and comes back. So really really avoid static classes !

这篇关于在ASP.NET静态对象的内存浪费?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆