2012-05-10 3 views
2

런타임시 이미지 크기 조정을 위해 .net dll (http://imageresizing.net/download)을 사용하고 있습니다. 그것은 완벽하게 작동합니다. 그러나, (1-7 사이의 일) 잠시 후 발생하는 시스템은 심지어 뷰어에서 예외를 발생하기 시작 :OutOfMemoryException in Image Resizing

예외 정보 : 예외 유형 :에서 OutOfMemoryException 예외 메시지 : 메모리가 부족하여 프로그램의 실행을 계속합니다 .

그런 예외가 발생하면 웹 사이트는 일반적으로 "System.OutOfMemoryException"을 던지는 오류로 인해 작업을 중단합니다.

웹 사이트가 실행되는 응용 프로그램 풀을 "재활용"하면 코드가 변경되지 않고 문제가 해결되고 웹 사이트가 즉시 정상 상태로 돌아갑니다.

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이고 예. 우리는 imageresizer와 함께 DiskCache 플러그인을 사용하고 있습니다. –

+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 참조).