System.OutOfMemoryException当CLR未能分配足够的内存时发生。System.OutOfMemoryException继承自System.SystemException类。OutOfMemoryException使用COR_E_OUTOFMEMORY值为0x8007000E的HRESULT。发生OutOfMemoryException的主要原因有两个:我们试图将StringBuilder对象扩展到超出其StringBuilder.MaxCapacity属性定义的长度。公共语言运行时无法分配足够的连续内存来成功执行操作。任何需要分配内存的属性赋值或方法调用都可能抛出此异常。设置字符串-stringStudentName="Tom";stringStudentSubject="数学";现在您需要使用分配的容量进行初始化,即初始值的长度-StringBuildersBuilder=newStringBuilder(StudentName.Length,StudentName.Length);现在,如果您尝试插入其他值,则会发生异常。sBuilder.Insert(值:StudentSubject,索引:StudentName.Length-1,计数:1);发生以下异常-System.OutOfMemoryException:内存不足要捕获错误,请尝试以下代码-示例:try{stringvideoSaveDir=CommonHelper.获取视频目录();int设置计数=0;#region模拟抛出OutOfMemoryException//ListdataSource=newList();//dataSource.Add(newVideoExtend(){EHost="http://www.baidu.com",FileName="BAI.mp4"});#endregionif(dataSource!=null){totalCount=dataSource.Count;}foreach(VideoExtendvideoindataSource){try{setCount++;字符串文件名=视频.文件名;字符串fileFullPath=videoSaveDir+fileName;if(File.Exists(fileFullPath)){if(!JudgeFileStatus(fileFullPath,fileName)){继续;}字符串strFileSize="";if(!FileCanUpload(fileFullPath,outstrFileSize)){//数据库更新上传失败,文件太大if(mongoData==null){apiHelper.UpdateUploadToQiniuFileTooLarge(video.EHost);}else{mongoData.UpdateUploadToQiniuFileTooLarge(video.EHost);}LogHelper.Log(LogFilePrefix+"uploadFileTooLarge","file"+fileName+"太大,大小:"+strFileSize);continue;}LogHelper.Log(LogFilePrefix+"uploadInfo","开始上传"+setCount+"/"+totalCount+"file:"+video.FileName);stringnewFileName="";boolupdateStatus=QiniuUtil.Upload(fileFullPath,outnewFileName);if(updateStatus){if(mongoData==null){apiHelper.UpdateUploadToQiniuSuccessStatus(video.EHost,newFileName);}else{mongoData.UpdateUploadToQiniuSuccessStatus(video.EHost,newFileName);//更新数据库}LogHelper.Log(LogFilePrefix+"uploadsuccess","上传成功,源文件名:"+video.FileName+";新文件名:"+newFileName);if(JudgeFileStatus(fileFullPath,fileName)){尝试{File.Delete(fileFullPath);}catch(Exceptionex){}}setCount++;}}else{//重置数据库重新下载if(mongoData==null){apiHelper.UpdateUploadToQiniuLocalFileNotFound(video.EHost);}别的{mongoData.UpdateUploadToQiniuLocalFileNotFound(video.EHost);}LogHelper.Log(LogFilePrefix+"uploadNoExisted","文件不存在:"+fileName);//thrownewSystem.OutOfMemoryException();//模拟抛出OutOfMemoryExceptionwith}}catch(System.OutOfMemoryExceptionmemoryEx){Global.IsOutOfMemoryException=true;LogHelper.LogWithLock(LogFilePrefix+"uploadOutOfMemoryException","失败,文件名"+video.FileName+",异常信息:"+memoryEx.Message+";内部错误"+memoryEx.InnerException?.Message);}catch(Exceptionex){LogHelper.Log(LogFilePrefix+"uploadError","Failed,filename"+video.FileName+",exceptionmessage:"+ex.Message+";InternalError"+ex.InnerException.Message);}System.Threading.Thread.Sleep(5*1000);//休眠}if(setCount<=0){LogHelper.Log(LogFilePrefix+"uploadInfo","没有要上传的新数据");}intsleepSecond=30;LogHelper.Log(LogFilePrefix+"uploadInfo","--sleep"+sleepSecond+"second");System.Threading.Thread.Sleep(sleepSecond*1000);//sleep}catch(Exceptionex){LogHelper.Log(LogFilePrefix+"uploadfullerror","上传失败,异常信息:"+ex.Message+";totalCount="+totalCount);上面处理OutOfMemoryException并生成以下错误输出结果:Error:Global.IsOutOfMemoryException=true;日志助手。LogWithLock(LogFilePrefix+"uploadOutOfMemoryException","失败,文件名"+video.FileName+",异常信息:"+memoryEx.Message+";内部错误"+memoryEx.InnerException?.Message);C#中catchoutofmemoryexceptions一文介绍介绍到此结束。关于C#捕捉内存异常的更多内容,请搜索往期文章或继续浏览以下相关文章。希望以后大家多多支持!本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: