2012-05-10 90 views
2

我們在運行時使用.net dll(http://imageresizing.net/download)進行imageresizing。它完美的作品。然而,過了一段時間(1-7天之間)發生,系統開始引發異常的甚至觀衆:OutOfMemoryException in Image Resizing

異常信息: 異常類型:OutOfMemoryException異常 異常消息:內存不足,無法繼續執行程序。

之後,該網站通常停止工作,拋出錯誤「System.OutOfMemoryException」。

如果我們「回收」網站正在運行的應用程序池,它會立即清除問題並且網站會立即恢復正常,而不會更改任何代碼。

imagereiszing dll之前,我們使用我們的自定義代碼,同樣的問題也發生在那。以下是代碼。

private Bitmap ConvertImage(Bitmap input, int width, int height, bool arc) 
    { 
     if (input.PixelFormat == PixelFormat.Format1bppIndexed || 
      input.PixelFormat == PixelFormat.Format4bppIndexed || 
      input.PixelFormat == PixelFormat.Format8bppIndexed) 
     { 

      Bitmap unpackedBitmap = new Bitmap(input.Width, input.Height); 
      Graphics g = Graphics.FromImage(unpackedBitmap); 
      g.Clear(Color.White); 
      g.DrawImage(input, new Rectangle(0,0,input.Width, input.Height)); 
      g.Dispose(); 
      input = unpackedBitmap; 
     } 


     double aspectRatio = (double)input.Height/(double)input.Width; 
     int actualHeight = CommonMethods.GetIntValue(Math.Round(aspectRatio * width, 0)); 


     Bitmap _imgOut; 

     if (actualHeight > height) 
     { 
      ResizeImage resizeImage = new ResizeImage(width, actualHeight, InterpolationMethod.Bicubic); 
      Bitmap _tempBitmap = resizeImage.Apply(input); 

      Bitmap _croppedBitmap = new Bitmap(width, height); 
      Graphics _crop = Graphics.FromImage(_croppedBitmap); 
      _crop.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
      _crop.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      _crop.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      _crop.DrawImageUnscaledAndClipped(_tempBitmap, new Rectangle(0, 0, width, height)); 
      _crop.Dispose(); 

      _imgOut = _croppedBitmap; 
     } 
     else 
     { 
      ResizeImage resizeImage = new ResizeImage(width, height, InterpolationMethod.Bicubic); 
      _imgOut = resizeImage.Apply(input); 
     } 

     // Draw the arc if it has been requested 
     if (arc) 
     { 
      Graphics _arc = Graphics.FromImage(_imgOut); 
      _arc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
      _arc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      _arc.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      _arc.DrawArc(new Pen(Color.White, 24), new Rectangle(-13, -13, 50, 50), 180, 90); 
      _arc.Dispose(); 
     } 

     // job done 
     return _imgOut; 
    } 

我們正在調整大小的圖像,如:www.mysite.com/images/myimage.jpg?width=196 &高度= 131

期待。 Farrukh

+0

有多少RAM?內存碎片可以減少大量可用塊,即使沒有發生內存泄漏。避免這種情況的最佳方法是確保您有足夠的RAM來支持您的方案。另外,您是否使用DiskCache插件?如果不是,那就是你的問題。 –

+0

感謝您的回覆。服務器上的RAM是:16GB,是的,我們正在使用DiskCache插件和imageresizer。 –

+0

您在這裏問2個不同的問題 - 1)我的代碼出了什麼問題,2)爲什麼我在ImageResizer中遇到問題?你還在使用你的舊代碼嗎?它確實有很多內存泄漏。 –

回答

2

當您遇到OutOfMemoryException(無論它出現在何處)時,它可能是由應用程序中任何位置的內存泄漏引起的。使用WinDbg調試了數十個這樣的實例後,我從未發現任何結果是由於http://imageresizing.net中的錯誤而導致的。

也就是說,有一個簡單的方法可以確定它是否是http://imageresizing.net的問題;在IIS中爲您的圖像和圖像調整大小創建一個單獨的應用程序池和子文件夾應用程序。除了圖像調整器之外,什麼都不安裝。下次您遇到錯誤時,請登錄到服務器並find out which w3wp.exe instance is responsible for the massive memory usage

如果它位於ImageResizer應用程序池中,請從http://imageresizing.net/support收集您的$ 20- $ 50缺陷獎金。如果沒有,你需要找出你在主應用程序中泄漏的東西。

如果您正在使用System.Drawing應用程序中的任何其他位置,那麼這是第一個查找的位置。 Check your code against this list of pitfalls

如果您肯定會在using或finally子句中處理每個System.Drawing。*實例,然後閱讀this excellent article a few times以確保您沒有在任何基礎上失敗,然後使用DebugDiag和/或WinDBG(請參閱bottom of article)。