我在我的 asp.net Web 服务应用程序中运行一个后台线程。该线程的职责是在特定时间后访问数据库并更新缓存中的数据表。数据表有大约 500K 行。在任务管理器中,当我查看进程时,Web 开发服务器第一次消耗大约 300,000K,下一次达到 500,000K,有时达到 1,000,000K 以上,有时又回落到 500,000-600,000K。当我在本地机器上工作时,数据库中的数据不会改变。谁能指导我在代码中做错了什么:
protected void Application_Start(object sender, EventArgs e)
{
Thread obj = new Thread(new ThreadStart(AddDataInCache));
obj.IsBackground = true;
obj.Start();
}
private void AddDataInCache()
{
Int32 iCount = 0;
while (true)
{
MyCollection _myCollection = new MyCollection();
DataTable dtReferences = null;
DataTable dtMainData = null;
try
{
dtMainData = _myCollection.GetAllDataForCaching(ref dtReferences);
HttpRuntime.Cache.Insert("DATA_ALL_CACHING", dtMainData, null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, null);
HttpRuntime.Cache.Insert("DATA_REFERENCES_CACHING", dtReferences, null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, null
);
}
catch (Exception ex)
{
}
finally
{
if (_myCollection != null)
_myCollection = null;
}
iCount++;
Thread.Sleep(18000);
}
}
在GetAllDataForCaching
我SqlDataReader
从我的数据访问层得到一个:
public DataTable GetAllDataForCaching(ref DataTable dReferenceTable)
{
DataTable dtReturn = new DataTable();
SqlDataReader dReader = null;
try
{
dReader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "[GetDataForCaching]", null);
if (dReader != null && dReader.HasRows)
{
dtReturn.Load(dReader);
dReferenceTable = new DataTable();
if (dReader.HasRows)
{
DataTable dtSchema = dReader.GetSchemaTable();
List<DataColumn> listCols = new List<DataColumn>();
if (dtSchema != null)
{
foreach (DataRow drow in dtSchema.Rows)
{
string columnName = System.Convert.ToString(drow["ColumnName"]);
DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"]));
column.Unique = (bool)drow["IsUnique"];
column.AllowDBNull = (bool)drow["AllowDBNull"];
column.AutoIncrement = (bool)drow["IsAutoIncrement"];
listCols.Add(column);
dReferenceTable.Columns.Add(column);
}
}
while (dReader.Read())
{
DataRow dataRow = dReferenceTable.NewRow();
for (int i = 0; i < listCols.Count; i++)
{
dataRow[((DataColumn)listCols[i])] = dReader[i];
}
dReferenceTable.Rows.Add(dataRow);
}
}
}
}
finally
{
if (dReader != null)
{
if (dReader.IsClosed == false)
dReader.Close();
dReader = null;
}
}
return dtReturn;
}
我正在使用 Visual Studio 2008。