나는이 코드의이 부분은 메모리 누수가 발생한다는 의심 :어떻게 메모리 누수 감지하는
public FileResult ShowCroppedImage(int id, int size)
{
string path = "~/Uploads/Photos/";
string sourceFile = Server.MapPath(path) + id + ".jpg";
MemoryStream stream = new MemoryStream();
var bitmap = imageManipulation.CropImage(sourceFile, size, size);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] bytes = stream.ToArray();
return File(bytes, "image/png");
}
가 어떻게이 코드 조각이 원인 인 경우 테스트가 볼 수 있도록 할 수 있습니까?
편집 :
public Image CropImage(string sourceFile, int newWidth, int newHeight)
{
Image img = Image.FromFile(sourceFile);
Image outimage;
int sizeX = newWidth;
int sizeY = newHeight;
MemoryStream mm = null;
double ratio = 0;
int fromX = 0;
int fromY = 0;
if (img.Width < img.Height)
{
ratio = img.Width/(double)img.Height;
newHeight = (int)(newHeight/ratio);
fromY = (img.Height - img.Width)/2;
}
else
{
ratio = img.Height/(double)img.Width;
newWidth = (int)(newWidth/ratio);
fromX = (img.Width - img.Height)/2;
}
if (img.Width == img.Height)
fromX = 0;
Bitmap result = new Bitmap(sizeX, sizeY);
//use a graphics object to draw the resized image into the bitmap
Graphics grPhoto = Graphics.FromImage(result);
//set the resize quality modes to high quality
grPhoto.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
//now do the crop
grPhoto.DrawImage(
img,
new System.Drawing.Rectangle(0, 0, newWidth, newHeight),
new System.Drawing.Rectangle(fromX, fromY, img.Width, img.Height),
System.Drawing.GraphicsUnit.Pixel);
// Save out to memory and get an image from it to send back out the method.
mm = new MemoryStream();
result.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
result.Dispose();
grPhoto.Dispose();
outimage = Image.FromStream(mm);
return outimage;
}
간단한 방법 : 프로그램을 반복적으로 호출하여 작업 관리자에서 프로세스를 시청하십시오. 사용 된 메모리가 계속 증가하고 절대로 떨어지지 않으면 누출이 발생합니다. – Blorgbeard
http://mitch-wheat.blogspot.com.au/2010/11/determine-if-your-net-application-has.html –
여기서 할 수있는 것은 MemoryStream을 using 블록에 래핑하여 처리하는 것입니다 자동으로. 왜 메모리 누수가 있다고 생각하니? imageManipulation.CropImage 안에 무엇이 있는지 알려주세요. 관리되지 않는 핸들을 닫고 있습니까? – abhishek