ASP.NET WebApp 性能问题:如何解决?

发布于 2024-12-01 00:08:46 字数 1643 浏览 1 评论 0原文

我一直在尝试找出 ASP.NET Web 应用程序的一些性能问题。它是一个在线应用程序,可供多个用户(10+)使用。快速概述:该应用程序使用 Web 表单、MVC 页面、Web 服务等的组合......当多个用户连接到该应用程序时,它似乎变得非常慢。在调查内存后,应用程序似乎使用了大量内存,这导致机器速度减慢,这表明未处理非托管资源。我安装了 ANTS,然后捕获了系统上的一些应用程序。事实证明,大量内存被非托管资源使用: http://tinypic.com/r/154ujra /7

这是我第一次分析内存。 ANTS 分析器表明我的一个类 (RULE) 具有大量活动实例:http://tinypic。 com/r/1264ltu/7 (这似乎没有被 GC 释放)

深入到类级别后,http://tinypic.com/r/2r3v6nq/7,它会显示一个警告,表明该类不是t 从内存中释放,这可能是因为事件处理程序未注销。现在该类确实包含一个事件处理程序实例,那么它可能是这样吗?

public class Rule
{
    public event EventHandler deleted;

    public void Delete()
    {
        if (baseQuestionnaire.basePortfolio.mode != Mode.Admin)
        {
            throw new Exception("Rules can only be deleted in Admin mode");
        }
        else
        {
            // Delete the rule from the database
            if (id != -1)
            {
                string delete = "DELETE FROM tb" + DataManager.dbPrefix + "_QuestionRule WHERE QuestionRuleId = " + id.ToString();
                DataManager.execute(delete);
            }

            // Raise a deleted event
            if (deleted != null)
                deleted(this, new EventArgs());
        }
    }
 } 

然后像这样将事件分配到另一个类中,但永远不会取消注册

public class Option : IComparable
{

    public void AddRule(Rule newRule)
    {
        newRule.deleted += new EventHandler(newRule_deleted);
        allRules.Add(newRule);
    }

    ............................
 }

I've been trying to identify some performance issues with our ASP.NET Web application. It's an online application, used by multiple users(10+). Just a quick overview: The applcation uses a combination of web forms, MVC Pages, web services etc... When multiple users connects to the app, it seems to become really slow. After investigating the memory, it seems as if the application is using A LOT of memory and that is slowing down the machine, which indicates unmanaged resources not being disposed of. I installed ANTS, and then captured a few applications on the system. It turns out a lot of the memory is used by unmanaged resources: http://tinypic.com/r/154ujra/7

This is the first time I've been profiling memory. ANTS profiler indicates that one of my classes (RULE) has a high number of live instances: http://tinypic.com/r/1264ltu/7 (Which doesn't seem to be freed up by the GC)

After drilling down into class level, http://tinypic.com/r/2r3v6nq/7, it displays a warning that the class isn't released from memory, and this could be because of an event handler not being unregistered. Now that class does contain an event handler instance, so could it be that?

public class Rule
{
    public event EventHandler deleted;

    public void Delete()
    {
        if (baseQuestionnaire.basePortfolio.mode != Mode.Admin)
        {
            throw new Exception("Rules can only be deleted in Admin mode");
        }
        else
        {
            // Delete the rule from the database
            if (id != -1)
            {
                string delete = "DELETE FROM tb" + DataManager.dbPrefix + "_QuestionRule WHERE QuestionRuleId = " + id.ToString();
                DataManager.execute(delete);
            }

            // Raise a deleted event
            if (deleted != null)
                deleted(this, new EventArgs());
        }
    }
 } 

Event is then assigned in another class like this, but never unregistered

public class Option : IComparable
{

    public void AddRule(Rule newRule)
    {
        newRule.deleted += new EventHandler(newRule_deleted);
        allRules.Add(newRule);
    }

    ............................
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-12-08 00:08:46

嗯..我看到“删除”的事件处理程序是公共事件处理程序。所以我的猜测是首先创建 Rule 对象,然后为 Rule.deleted 分配一个事件处理程序。如果是这种情况,那么正如您所怀疑的那样,事件处理程序可能是规则对象未被垃圾收集的原因。

编辑:

也许你可以尝试这样的事情:

public class Option : IComparable, IDisposable
{

    private Rule newRule;
    private EventHandler newRuleDeletedEventHandler;

    public void AddRule(Rule newRule)
    {
        this.newRule = newRule;
        newRuleDeletedEventHandler = new EventHandler(newRule_deleted);
        newRule.deleted += newRuleDeletedEventHandler;
        allRules.Add(newRule);
    }

    public override void dispose()
    {
        newRule.deleted -= newRuleDeletedEventHandler;
    }
}

Hmm.. I see that the eventhandler 'deleted' is a public one. So my guess is that the Rule object is created first and then the Rule.deleted is assigned an eventhandler. If this is the case then as you suspected, the eventhandler is probably the cause of Rule objects not being garbage collected.

EDIT:

Maybe you could try something like this:

public class Option : IComparable, IDisposable
{

    private Rule newRule;
    private EventHandler newRuleDeletedEventHandler;

    public void AddRule(Rule newRule)
    {
        this.newRule = newRule;
        newRuleDeletedEventHandler = new EventHandler(newRule_deleted);
        newRule.deleted += newRuleDeletedEventHandler;
        allRules.Add(newRule);
    }

    public override void dispose()
    {
        newRule.deleted -= newRuleDeletedEventHandler;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文