我有一臺數據庫運行在一臺機器上,用於逐秒更新信息。我的所有客戶提供的數據都返回到一個源,然後將數據發佈到數據庫。所有這些都很好,我遇到的問題是我的服務代理和聆聽變化。SQL Server 2008 R2快速服務代理正在使用所有空閒內存
我正在使用SqlDependency對象來註冊特定查詢的偵聽器。我按預期得到事件,但服務器最終變得無法使用。我能夠確定Service Broker正在使用超過900MB的內存(推動我超過1GB的限制)。我有這樣的印象:我的事件一直停留在記憶中,沒有清理出去。每次我收到一個事件時,我都會清除該事件監聽器並註冊一個新事件。有沒有更好的方法來清理數據庫中的事件?
另外,我已經讀過,你必須在每次註冊一個新事件時都要叫Stop and Start。根據我的經驗,再次呼叫停止總是永遠掛起。另外,如果停止清除所有事件,我有多個聽衆,並且當我收到其他人時,我不想停止其他人。
這裏是我使用的註冊和響應事件的代碼:
using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.DatabseEventConnectionString))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Field1, Field2, Field3, Field4 FROM dbo.Table";
cmd.Notification = null;
SqlDependency dep = new SqlDependency(cmd);
dep.OnChange += new OnChangeEventHandler(dependency_OnChange);
cn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Handle read here;
}
}
}
void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
// If InvokeRequired returns True, the code
// is executing on a worker thread.
if (Dispatcher.CheckAccess())
{
SqlDependency dep = sender as SqlDependency;
dep.OnChange -= new OnChangeEventHandler(dependency_OnChange);
RegisterTableListener();
}
else
{
// Create a delegate to perform the thread switch.
OnChangeEventHandler tempDelegate =
new OnChangeEventHandler(dependency_OnChange);
object[] args = { sender, e };
// Marshal the data from the worker thread
// to the UI thread.
Dispatcher.Invoke(tempDelegate, args);
}
}
任何想法,爲什麼內存是永攀?
正在運行測試和內存只是永遠不會下降。即使在啓動一個新實例後關閉了我的應用程序並調用了停止之後,內存仍會繼續攀升並永遠不會釋放。 – JeremyK 2011-12-13 20:44:48
我們與服務代理人有類似的問題,我相信它的應用程序並沒有結束它創建的對話。 – user957902 2011-12-13 22:16:02