[英]IIS and IIS express memory leak
我有和asp.net mvc 3網站。 在本地VS Web服務器或IIS上運行時,表示可以。 但是,在IIS(IIS 7.5 Windows 2008 R2)上運行它時,似乎內存泄漏,因為內存使用量一直在增長。 有任何想法嗎?
還有一個更新:應用中有這樣的代碼:
SqlConnection conn = new SqlConnection { //creating connection here };
conn.Open();
SqlCommand command = conn.CreateCommand();
try
{
var reader = command.ExecuteReader();
while (reader.Read())
{
//read the data
}
}
finally
{
conn.Close();
}
也許應該有類似Reader之類的東西。 可能是內存泄漏的原因嗎?
更新:由於某種原因,gc.Collect解決了該問題。 但這並不是出路,因為一直調用gc.collect是一個壞主意。
最好的最佳做法是使用方括號,這是c#的重要功能。 當將“ using”關鍵字與方括號一起使用時,超出方括號范圍時,它將自動處置使用的對象。 這是一個例子;
// SqlConnection implements IDisposable, will be disposed after bracket is closed
using(SqlConnection conn = new SqlConnection())
{
conn.Open();
// SqlCommand implements IDisposable, will be disposed after bracket is closed
using(SqlCommand command = conn.CreateCommand())
{
// DataReader implements IDisposable, will be disposed after bracket is closed
using(var reader = command.ExecuteReader())
{
while (reader.Read())
{
// read here.
}
}
}
}
這也是microsoft鏈接,上面寫着“在using塊結束時,連接自動關閉。” http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.close%28v=VS.85%29.aspx
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.