C# 释放内存,静态类析构函数?

C# 释放内存,静态类析构函数?,c#,asp.net,memory-management,C#,Asp.net,Memory Management,我有一个静态类,我用它作为我网站的数据层。在这个类中,我有一个字符串数组,它存储了我以后可以访问的查询信息。以下是我的课程部分和讨论的方法: public static class data_layer { private static string[] items; private static string[] description; //will return description for an item id. if no item id is found,

我有一个静态类,我用它作为我网站的数据层。在这个类中,我有一个字符串数组,它存储了我以后可以访问的查询信息。以下是我的课程部分和讨论的方法:

public static class data_layer
{
    private static string[] items;
    private static string[] description;

    //will return description for an item id. if no item id is found, null is returned
    public static string getDesc(string key)
    {
        int i = 0;
        bool flag = false;
        //search for the item id to find its index
        for(i = 0; i < items.Length; i++)
        {
            if(items[i] == key)
            {
                flag = true;
                break;
            }
        }
        if(flag)
            return description[i];
        else
            return null;
    }
    public static string[] getItems()
    {
        return items;
    }

    public static bool setItemsAndDescriptions()
    {
        ArrayList itemIDs = new ArrayList();
        ArrayList itemDescs = new ArrayList();

        SqlConnection sqlConn = new SqlConnection();
        sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings["MAS200RAWConnectionString"].ConnectionString;
        string query = "SELECT ItemNumber, ItemDescription FROM OUS_IM1_InventoryMasterfile " +
            "WHERE ItemNumber LIKE 'E%' OR ItemNumber LIKE 'B%' OR ItemNumber LIKE 'D%'";

        try
        {
            sqlConn.Open();
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandType = CommandType.Text;
            sqlComm.CommandText = query;
            SqlDataReader reader = sqlComm.ExecuteReader();
            if (reader == null)
                return false;

            //add the queried items to the ddl
            while (reader.Read())
            {
                itemIDs.Add(reader["ItemNumber"].ToString().Trim());
                itemDescs.Add(reader["ItemDescription"].ToString().Trim());
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            sqlConn.Close();   //NOTE: I HAVE A BREAKPOINT HERE FOR DUBUGGING
        }

        items = itemIDs.ToArray(typeof(string)) as string[];
        description = itemDescs.ToArray(typeof(string)) as string[];
        return true;
    }
}
公共静态类数据\u层
{
私有静态字符串[]项;
私有静态字符串[]描述;
//将返回项目id的说明。如果未找到项目id,则返回null
公共静态字符串getDesc(字符串键)
{
int i=0;
布尔标志=假;
//搜索项目id以查找其索引
对于(i=0;i

这一切都很好,但通过将断点放在我说的地方,我注意到类成员项和描述在我的程序(本地asp开发服务器)执行之间保留了分配的内存和元素。为什么程序结束时(退出浏览器或停止调试模式)内存没有释放?有没有办法手动释放此内存并为静态类生成析构函数?

没有,静态类没有析构函数,但您可以执行以下操作:

public static void Unload() {
    items = description = null;
}

Re“为什么程序结束时内存没有释放”——如果你是说退出浏览器,服务器甚至不会注意到这一点。当应用程序池(在IIS中)死亡时,它将被清除。

这是因为字段是静态的,停止调试并不意味着WebDev服务器已关闭。
如果要按用户存储字符串,请将其放入会话对象中。它保证每个用户都可以使用这些代码,并且在会话结束时(超时或关闭浏览器窗口)会被遗忘。

顺便说一句,这些代码是完全非线程安全的(静态方法通常应该是线程安全的),而且,由于从Classic中返回
,数据有可能发生意外变异。我真的对线程一无所知。最近毕业,在编程专业的4年中从未提到过多线程。。uber对此非常生气这是一个离题的话题,但你的代码看起来很糟糕。除非使用.NET 1.1,否则请将ArrayList替换为其通用版本列表。其次,标志变量是无用的。尝试重构代码。还可以看看C#命名转换,因为您似乎是Java开发人员:Chris是对的。不要被鼓励。将我的评论视为旨在帮助您编写更好代码的建议。事实上,我们每个人都以现在的方式编写代码,但是经过几年的经验之后,你就会明白应该怎么做。祝你好运@詹德拉斯-你是说“不要泄气”?哦,我明白了。我想当你退出或浏览网站时,服务器会终止应用程序并为该用户释放所有内存。谢谢,知道def将帮助编写我的代码:)顺便说一句,我假设我必须自己调用Unload(),它不会像析构函数一样自动执行?@nicklamart-请记住,您的所有用户都使用相同的静态数据。为用户A卸载它也就是为用户B卸载它,用户CMarc在3个浏览器中的所有14个选项卡,如果在应用程序池死亡之前我的数据都没有被清理,是否有可能在数据库中输入一条新记录,并且用户会尝试重新加载页面,将一个新大小的数组加载到我已经定义的数组中并抛出一个错误?如果是这样的话,将其设置为null会允许重新分配吗?哈哈,我知道现在我知道它将不会免费,直到应用程序池死亡,thx^^