我有懷疑,這部分代碼會導致內存泄漏:如何檢測內存泄露
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 2012-02-13 23:29:51
http://mitch-wheat.blogspot.com.au/2010/11/determine-if-your-net-application-has.html – 2012-02-13 23:30:36
,你可以在這裏做的唯一一件事就是包裹的MemoryStream到使用塊處置它會自動。你爲什麼認爲有任何內存泄漏。請讓我們知道imageManipulation.CropImage裏面有什麼。你關閉非託管手柄嗎? – abhishek 2012-02-13 23:31:26