垃圾收集器如何在ASP.NET MVC中处理会话 [英] How will the Garbage Collector handle a Session in ASP.NET MVC

查看:104
本文介绍了垃圾收集器如何在ASP.NET MVC中处理会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public Dictionary< string,IMYObject> MYObjectContainer 
{
get
{
if(Session [MYObjectPool] == null)
Session [MYObjectPool] = new Dictionary< string,IMYObject> ();
return(Dictionary< string,IMYObject>)Session [MYObjectPool];
}
set
{
Session [MYObjectPool] = value;


$ b $ public ActionResult Close()
{
try
{
MyObject obj = this.MYObjectContainer [键]
this.MYObjectContainer = null;
返回Json(new {success = true},JsonRequestBehavior.AllowGet);

catch(Exception Ex)
{
throw X
}
}

当对象没有有效裁判时,垃圾收集器将会删除
这里有两个裁判,



1.bj(局部变量)

2.Session



首先,我通过设置会话裁判无效 this.MYObjectContainer = null;
第二次当函数结束时,obj将弹出堆栈,因此第二裁判无效

这是否使 MYObjectContainer 符合垃圾收集器的要求?



请忽略if我的问题是完全错误的请指教我?



垃圾收集器如何在ASP.NET会话中工作?

解决方案

在上面的示例中,会话超时之前Session对象不会被垃圾收集。



您必须决定是否这样是你想要的b行为与否:) - 你会在晚些时候需要物体吗?



如果你想从Session中删除对象,你还必须写 Session [MYObjectPool] = null Session.Remove(MYObjectPool)(它也会这样)

在Session中放置并不是问题,但是如果对象很大(例如兆字节或甚至千兆字节)和/或您有很多用户(所有人都将获得他们自己的会话),或者在具有对象铺设的同一服务器上的许多站点

Session很方便,但你必须意识到它的局限性......


public Dictionary<string, IMYObject> MYObjectContainer
{
    get
    {
        if (Session["MYObjectPool"] == null)
            Session["MYObjectPool"] = new Dictionary<string,IMYObject>();
        return (Dictionary<string, IMYObject>)Session["MYObjectPool"];
    }
    set
    {
        Session["MYObjectPool"] = value;
    }
}

public ActionResult Close()
{
    try
    {
        MyObject obj = this.MYObjectContainer["Key"] 
        this.MYObjectContainer = null;             
        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception Ex)
    {
        throw X
    }
}

The garbage collector will delete when the object has no valid referee Here there are two referee,

1.obj (Local variable)

2.Session

First I made the session referee invalid by setting this.MYObjectContainer = null; Second when the function ends the obj will be popped out of stack thus second referee is invalid

Does this makes the MYObjectContainer eligible for Garbage Collector to be Cleared ?

Please ignore if my question is totally wrong please advice me ?

How Garbage Collector works in ASP.NET Session ?

解决方案

In your example above the Session object will not be garbage collected until the session times out.

You'll have to decide if this is your wanted behavior or not :) - Will you need the object later?

If you want to remove the object from Session you'll also have to write Session["MYObjectPool"] = null or Session.Remove("MYObjectPool") (which will do the same)

Many times having objects laying around in Session is not a problem, but if the objects are large (e.g. megabytes or even gigabytes) and/or you have lots of users (all of who will get their own session) or many sites on the same server having objects laying around will be a problem.

Session is handy, but you have to realize its limitations...

这篇关于垃圾收集器如何在ASP.NET MVC中处理会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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