当clr未能分配所需的足够内存时,将发生system.outofmemoryexception
。system.outofmemoryexception
继承自system.systemexception
类。outofmemoryexception
使用cor_e_outofmemory
值为 0x8007000e的 hresult 。
一个outofmemoryexception异常异常主要有两个原因:
我们试图将stringbuilder
对象扩展到超出其stringbuilder.maxcapacity
属性定义的长度。
公共语言运行时无法分配足够的连续内存来成功执行操作。任何需要分配内存的属性分配或方法调用都可能引发此异常。
设置字符串-
string studentname = "tom";string studentsubject = "maths";
现在您需要使用分配的容量进行初始化,该容量是初始值的长度-
stringbuilder sbuilder = new stringbuilder(studentname.length, stude教材教法ntname.length);
现在,如果您尝试插入其他值,则会发生异常。
sbuilder.inrt(value: studentsubject, index: studentname.length - 1, count: 1);
发生以下异常-
system.outofmemoryexception: out of memory
要捕获错误,请尝试以下代码-
示例:
try { string videosavedir = commonhelper.getvideodirectory(); int tcount = 0; #region 模拟抛出outofmemoryexception用 //list<videoextend> datasource = new list<videoextend>(); //datasource.add(new videoextend() { ehost="http://www.baidu.com",filename="bai.mp4"}); #endregion if (datasource != null) { totalcount = datasource.count; } foreach (videoextend video in datasource) { try { tcount++; string filename = video.filename; string filefullpath = videosavedir + filename; if (file.exists(filefullpath)) { if (!judgefilestatus(filefullpath, filename)) { continue; } string strfilesize = ""; if (!filecanupload(filefullpath, out strfilesize)) { //数据库更新为上传南开大学分数线失败,文件太大 if (mongodata == null) { apihelper.updateuploadtoqiniufiletoolarge(video.ehost)behind的反义词; } el { mongodata.updateuploadtoqiniufiletoolarge(video.ehost); } loghelper.log(logfileprefix+"uploadfiletoolarge", "文件" + filename + "太大,大小为:" + strfilesize); continue; } loghelper.log(logfileprefix + "uploadinfo", "开始上传" + tcount + "/" + totalcount + "文件:" + video.filename); string newfilename = ""; bool updatestatus = qiniuutil.upload(filefullpath, out newfilename); if (updatestatus) { if (mongodata == null) { apihelper.updateuploadtoqiniusuccessstatus(video.ehost, newfilename); } el 继续教育考试答案 { mongodata.updateuploadtoqiniusuccessstatus(video.ehost, newfilename);//更新数据库 } loghelper.log(logfileprefix + "uploadsuccess", "上传成功,源文件名:" + video.filename + ";新文件名:" + newfilename); if (judgefilestatus(filefullpath, filename)) { try { file.delete(filefullpath); } catch (exception ex) { } } tcount++; } } el { //把数据库重置为要重新下载 if (mongodata == null) { apihelper.updateuploadtoqiniulocalfilenotfound(video.ehost); } el { mongodata.updateuploadtoqiniulocalfilenotfound(video.ehost); } loghelper.log(logfileprefix + "uploadnoexisted", "文件不存在:" + filename); //throw new system.outofmemoryexception();//模拟抛出outofmemoryexception用 } } catch (system.outofmemoryexception memoryex) { global.isoutofmemoryexception = true; loghelper.logwithlock(logfileprefix + "uploadoutofmemoryexception", "失败,文件名" + video.filename + ",异常信息:" + memoryex.message + ";内部错误" + memoryex.innerexception?.message); } catch (exception ex) { loghelper.log(logfileprefix + "uploaderror", "失败,文件名" + video.filename + ",异常信息:" + ex.message + ";内部错误" + ex.innerexception.message); } system.threading.thread.sleep(5 * 1000);//休眠 } if (tcount <= 0) { loghelper.log(logfileprefix + "uploadinfo", "暂无新待上传数据"); } in宁夏理工学院t sleepcond = 30; loghelper.log(logfileprefix + "uploadinfo", "--休眠" + sleepcond + "秒"); system.threading.thread.sleep(sleepcond * 1000);//休眠 } catch (exception ex) { loghelper.log(logfileprefix + "uploadfullerror", "失败,异常信息:" + ex.message+ ";totalcount="+ totalcount); }
上面处理outofmemoryexception并生成以下错误-
输出结果:
error:
global.isoutofmemoryexception = true; loghelper.logwithlock(logfileprefix + "uploadoutofmemoryexception", "失败,文件名" + video.filename + ",异常信息:" + memoryex.message + ";内部错误" + memoryex.innerexception?.message);
到此这篇关于在c#中捕获内存不足异常的文章就介绍到这了,更多相关c#捕获内存异常内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 12:56:29,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/291632809e4b9786b14507ea71485e4e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:在C#中捕获内存不足异常.doc
本文 PDF 下载地址:在C#中捕获内存不足异常.pdf
留言与评论(共有 0 条评论) |