在C#中捕獲內(nèi)存不足異常
當(dāng)CLR未能分配所需的足夠內(nèi)存時(shí),將發(fā)生System.OutOfMemoryException
。System.OutOfMemoryException
繼承自System.SystemException
類(lèi)。OutOfMemoryException
使用COR_E_OUTOFMEMORY
值為 0x8007000E的 HRESULT 。
一個(gè)OutOfMemoryException異常異常主要有兩個(gè)原因:
我們?cè)噲D將StringBuilder
對(duì)象擴(kuò)展到超出其StringBuilder.MaxCapacity
屬性定義的長(zhǎng)度。
公共語(yǔ)言運(yùn)行時(shí)無(wú)法分配足夠的連續(xù)內(nèi)存來(lái)成功執(zhí)行操作。任何需要分配內(nèi)存的屬性分配或方法調(diào)用都可能引發(fā)此異常。
設(shè)置字符串-
string StudentName = "Tom"; string StudentSubject = "Maths";
現(xiàn)在您需要使用分配的容量進(jìn)行初始化,該容量是初始值的長(zhǎng)度-
StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length);
現(xiàn)在,如果您嘗試插入其他值,則會(huì)發(fā)生異常。
sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1);
發(fā)生以下異常-
System.OutOfMemoryException: Out of memory
要捕獲錯(cuò)誤,請(qǐng)嘗試以下代碼-
示例:
try { string videoSaveDir = CommonHelper.GetVideoDirectory(); int setCount = 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 { setCount++; string fileName = video.FileName; string fileFullPath = videoSaveDir + fileName; if (File.Exists(fileFullPath)) { if (!JudgeFileStatus(fileFullPath, fileName)) { continue; } string strFileSize = ""; if (!FileCanUpload(fileFullPath, out strFileSize)) { //數(shù)據(jù)庫(kù)更新為上傳失敗,文件太大 if (mongoData == null) { apiHelper.UpdateUploadToQiniuFileTooLarge(video.EHost); } else { mongoData.UpdateUploadToQiniuFileTooLarge(video.EHost); } LogHelper.Log(LogFilePrefix+"uploadFileTooLarge", "文件" + fileName + "太大,大小為:" + strFileSize); continue; } LogHelper.Log(LogFilePrefix + "uploadInfo", "開(kāi)始上傳" + setCount + "/" + totalCount + "文件:" + video.FileName); string newFileName = ""; bool updateStatus = QiniuUtil.Upload(fileFullPath, out newFileName); if (updateStatus) { if (mongoData == null) { apiHelper.UpdateUploadToQiniuSuccessStatus(video.EHost, newFileName); } else { mongoData.UpdateUploadToQiniuSuccessStatus(video.EHost, newFileName);//更新數(shù)據(jù)庫(kù) } LogHelper.Log(LogFilePrefix + "uploadsuccess", "上傳成功,源文件名:" + video.FileName + ";新文件名:" + newFileName); if (JudgeFileStatus(fileFullPath, fileName)) { try { File.Delete(fileFullPath); } catch (Exception ex) { } } setCount++; } } else { //把數(shù)據(jù)庫(kù)重置為要重新下載 if (mongoData == null) { apiHelper.UpdateUploadToQiniuLocalFileNotFound(video.EHost); } else { 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 + ";內(nèi)部錯(cuò)誤" + memoryEx.InnerException?.Message); } catch (Exception ex) { LogHelper.Log(LogFilePrefix + "uploadError", "失敗,文件名" + video.FileName + ",異常信息:" + ex.Message + ";內(nèi)部錯(cuò)誤" + ex.InnerException.Message); } System.Threading.Thread.Sleep(5 * 1000);//休眠 } if (setCount <= 0) { LogHelper.Log(LogFilePrefix + "uploadInfo", "暫無(wú)新待上傳數(shù)據(jù)"); } int sleepSecond = 30; LogHelper.Log(LogFilePrefix + "uploadInfo", "--休眠" + sleepSecond + "秒"); System.Threading.Thread.Sleep(sleepSecond * 1000);//休眠 } catch (Exception ex) { LogHelper.Log(LogFilePrefix + "uploadfullerror", "失敗,異常信息:" + ex.Message+ ";totalCount="+ totalCount); }
上面處理OutOfMemoryException并生成以下錯(cuò)誤-
輸出結(jié)果:
Error:
Global.IsOutOfMemoryException = true; LogHelper.LogWithLock(LogFilePrefix + "uploadOutOfMemoryException", "失敗,文件名" + video.FileName + ",異常信息:" + memoryEx.Message + ";內(nèi)部錯(cuò)誤" + memoryEx.InnerException?.Message);
到此這篇關(guān)于在C#中捕獲內(nèi)存不足異常的文章就介紹到這了,更多相關(guān)C#捕獲內(nèi)存異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于c#用Socket做一個(gè)局域網(wǎng)聊天工具
目前基于Internet的即時(shí)聊天工具已經(jīng)做的非常完美,本文介紹了基于c#用Socket做一個(gè)局域網(wǎng)聊天工具,有需要的朋友可以看一下。2016-10-10c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
這篇文章主要介紹了c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼,有需要的朋友可以參考一下2013-12-12c#之OpenFileDialog解讀(打開(kāi)文件對(duì)話(huà)框)
這篇文章主要介紹了c#之OpenFileDialog(打開(kāi)文件對(duì)話(huà)框),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07C#中datagridview的EditingControlShowing事件用法實(shí)例
這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實(shí)例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下2015-06-06C# Char結(jié)構(gòu)中IsLetterOrDigit(Char)的方法詳解
這篇文章給大家介紹了C#的Char 結(jié)構(gòu)的IsLetterOrDigit(Char)的方法,并通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02