如何通过配置静态方法prevent故障(内存泄漏)? [英] How to prevent malfunction (memory leak) by disposing static methods?

查看:156
本文介绍了如何通过配置静态方法prevent故障(内存泄漏)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET 4.0。

I am using ASP.NET 4.0.

从过去的几个星期,一些用户抱怨说,应用程序启动故障。在GridView突然开始显示出相同的用户或其他并发用户可能会在这一天任何时间点访问过一个下拉控制内容。同样,下拉控件可以通过的RowID得到填充任何旧的结果集的实际项目来代替。

From last few weeks, few users are complaining that the application starts malfunctioning. The GridView suddenly starts showing contents of a DropDown control that the same user or another concurrent user might have accessed at any point of time in that day. Similarly, DropDown controls may get populated by RowID of the any old result-set instead of the actual items.

我读到一篇文章:<一href=\"http://blogs.msdn.com/b/jorman/archive/2005/11/22/users-seeing-other-users-data-in-asp-net.aspx\"相对=nofollow>看到ASP.NET中的其他用户的数据的用户在这里笔者讨论了静态对象负责的内存泄漏问题。

I came across an article: Users seeing other users data in ASP.NET where the author discusses about Static objects responsible for memory leak behavior.

这让我想起在我的项目一类,这是静态并包含公共静态方法我。这个类包含的方法基于查询的输入来填充下拉列表中,返回一个数据集的查询输入或返回一个标对象。

It reminds me of a class in my project which is Static and contains public static methods. This class contains methods to populate a DropDown, return a DataSet for a query input or return a scalar object based on a query input.

这个类的提取物是如下:

Extract of this class is below:

public static class reuse
{
    public static void FillDropDownList(string Query, DropDownList DropDownName, string ConnectionStringParameter)
    {
        SqlDataReader dr;

        try
        {
            dbConnection.OpenConnection(ConnectionStringParameter);

            //Check whether the Drop Down has existing items. If YES, empty it.
            if (DropDownName.Items.Count > 0)
                DropDownName.Items.Clear();

            SqlCommand cmd = new SqlCommand(Query,dbConnection.cn);
            dr = cmd.ExecuteReader();

            DropDownName.Items.Add("-- Select --");
            DropDownName.Items.Add("All");
            while (dr.Read())
                DropDownName.Items.Add(dr[0].ToString());

            dr.Close();
        }
        catch (Exception ex)
        {
            rpkCustomErrorHandler.GetScript(HttpContext.Current.Response,ex.Message.ToString());
        }
        dbConnection.CloseConnection();
    }
}

我想知道这是否是我上面所讨论的故障原因。如果是的话,有没有办法处置静态方法的方法的任务已经完成了。我需要从静态类更改为默认的普通类?

I want to know whether this is the cause of the malfunction I discussed above. If yes, is there any way to dispose Static Methods after the task of the method has finished. Do I need to change the class from Static to a default plain class?

编辑

我有另一个类,这也是静态的,且由上面的类:

I have another class which is also static and is used by the above class:

public static class dbConnection
{
    public static SqlConnection cn = new SqlConnection();

    public static void OpenConnection()
    {
        try
        {
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["cnWebTwDrill"].ToString();

            if (cn.State == ConnectionState.Closed)
                cn.Open();
        }
        catch (Exception)
        {
            throw;              
        }
    }
}

我必须从连接类中删除静态和每次使用一个唯一的实例拨打电话这一类?

Shall I remove "Static" from the Connection Class and make call to this class by using a unique instance each time?

推荐答案

您已经证明我们的code是静态类/方法的不好的用法的一个例子。你把一个静态方法或类的事情是简单的以适应一个对象,它不需要状态。

The code you've shown us is an example of bad usage of static class/method. You put in a static method or class things that are to simple to fit in an object AND which DON'T REQUIRE state.

您应该在您的数据分配给服务器控件的业务层(这将检索数据)和UI /控制器(在这种情况下页)之间被分割的功能。所有此操作是特定的请求,也没有理由使用的静态方法为该。仅仅是一个(不好的)过程编程的迹象。并与数据库访问(或一次性对象)打交道时,你应该使用的使用的声明。事情是这样的。

The functionality you have there should be split between the Business Layer (which will retrieve the data) and the UI/COntroller (in this case Page ) where you assign the data to the server control. All this operations are specific to a request, there is no reason to use a static method for that. Is just a sign of (bad) procedural programming. And when dealing with db access (or disposable objects) you should use the using statement. Something like this

using(var conex=GetConnection())
{
   try
   {
     conex.Open(); 
     //do stuff

    var cmd= new SqlCommand();//cfg command
    using (var rd= cmd.ExecuteReader())   
      {
           //do read
      }
   }
   catch(Exception ex)
    {
       //handle exception
    }
}

使用的声明会自动在块的结尾调用Dispose。这基本上是一个快捷方式

The using statement automatically calls Dispose at the end of the block. It's basically a shortcut for

try {}
finally{ //dispose }.

这篇关于如何通过配置静态方法prevent故障(内存泄漏)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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