I have a static class that I use as my data layer on my website. In this class, I have string arrays that store the requested data, which I can access later. Here is part of my class and the method in question:
public static class data_layer
{
private static string[] items;
private static string[] description;
public static string getDesc(string key)
{
int i = 0;
bool flag = false;
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;
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();
}
items = itemIDs.ToArray(typeof(string)) as string[];
description = itemDescs.ToArray(typeof(string)) as string[];
return true;
}
}
This all works fine, but by setting a breakpoint where I said what I said, I noticed that the elements and the description of the class elements retain the allocated memory and elements between the execution of my program (the local asp dev server). Why is this memory not freed when the program ends (exiting the browser or stopping debugging mode)? Is there a way to manually free this memory and create a handle for a static class?