У меня есть статический класс, который я использую в качестве слоя данных на моем сайте. В этом классе у меня есть строковые массивы, которые хранят запрашиваемую информацию, к которой я могу получить доступ позже. Вот часть моего класса и рассматриваемый метод:
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;
}
}
Это все работает нормально, но, поставив точку останова там, где я сказал, что я ее поставил, я заметил, что элементы и описание членов класса сохраняют выделенную память и элементы между выполнениями моей программы (локальный сервер asp dev). Почему эта память не освобождается, когда программа заканчивается (выход из браузера или остановка режима отладки)? Есть ли способ вручную освободить эту память и создать десктруктор для статического класса?