此问题网上很多人都遇到过,百度出来的解决方案似乎都不能解决,请高手帮忙。程序代码就一行:System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("/up.jpg"));
图片大小:3.3MB 尺寸:3648*2736 正常的数码照片
经过测试当回收工作进程的时候,运行正常,当内存到达一定值的时候,问题就出现这个异常了。
在相同环境下,改变图片的尺寸也可以运行,比如用PS缩小到宽为3500px的时候正常,3057px的时候就不正常,且此数值不固定。后来想到一个办法,就是当内存达到800M后自动回收工作进程,运行一天,没有问题,晚上就出现了一个莫名其妙的问题:“字符串转换时间函数Convert.ToDateTime”报错,所有用到的地方都提示“该字符串未被识别为有效的 DateTime”,随后,回收工作进程,一切正常,真郁闷了,也不知道和内存自动回收有没有关系。
运行后出现下面的错误提示:Server Error in '/' Application.
--------------------------------------------------------------------------------Out of memory.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.OutOfMemoryException: Out of memory.
Stack Trace:
[OutOfMemoryException: Out of memory.]
System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +380120
System.Drawing.Image.FromFile(String filename) +6
ASP.up_aspx.Button1_Click(Object sender, EventArgs e) +26
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
图片大小:3.3MB 尺寸:3648*2736 正常的数码照片
经过测试当回收工作进程的时候,运行正常,当内存到达一定值的时候,问题就出现这个异常了。
在相同环境下,改变图片的尺寸也可以运行,比如用PS缩小到宽为3500px的时候正常,3057px的时候就不正常,且此数值不固定。后来想到一个办法,就是当内存达到800M后自动回收工作进程,运行一天,没有问题,晚上就出现了一个莫名其妙的问题:“字符串转换时间函数Convert.ToDateTime”报错,所有用到的地方都提示“该字符串未被识别为有效的 DateTime”,随后,回收工作进程,一切正常,真郁闷了,也不知道和内存自动回收有没有关系。
运行后出现下面的错误提示:Server Error in '/' Application.
--------------------------------------------------------------------------------Out of memory.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.OutOfMemoryException: Out of memory.
Stack Trace:
[OutOfMemoryException: Out of memory.]
System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +380120
System.Drawing.Image.FromFile(String filename) +6
ASP.up_aspx.Button1_Click(Object sender, EventArgs e) +26
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
如果文件没有有效的图像格式,或者如果 GDI+ 不支持文件的像素格式,则此方法将引发 OutOfMemoryException 异常
检查datetime数据
/// <summary>
/// 得到格式 yyyy-MM-dd HH:mm:ss.fff 日期型, 是否补0、是否有时间、是否有毫秒都行
/// 支持只要是按照 年 月 日 小时 分钟 秒 毫秒 排列的任何字符串,分隔符可以是 ' ', '-', ':', '.', '/', '\\', ','
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static DateTime GetDateTime_Cn(string value)
{
string[] valueItem = value.Split(new char[] { ' ', '-', ':', '.', '/', '\\', ',' });
return new DateTime(Convert.ToInt32(valueItem[0]), Convert.ToInt32(valueItem[1]), Convert.ToInt32(valueItem[2]),
valueItem.Length > 3 ? Convert.ToInt32(valueItem[3]) : 0,
valueItem.Length > 4 ? Convert.ToInt32(valueItem[4]) : 0,
valueItem.Length > 5 ? Convert.ToInt32(valueItem[5]) : 0,
valueItem.Length > 6 ? Convert.ToInt32(valueItem[6]) : 0);
}
----------------------------------------------------------------------------------
这行代码是存在内在泄露的问题,特别是如果你的路径不存在这个文件时最明显,之前我也遇到过类似的情况,在这段代码前加一个try就行了!如:
Try
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("/up.jpg"));}
Catch
{
//不成功
}