在C#中捕獲內(nèi)存不足異常
當(dāng)CLR未能分配所需的足夠內(nèi)存時,將發(fā)生System.OutOfMemoryException。System.OutOfMemoryException繼承自System.SystemException類。OutOfMemoryException使用COR_E_OUTOFMEMORY值為 0x8007000E的 HRESULT 。
一個OutOfMemoryException異常異常主要有兩個原因:
我們試圖將StringBuilder對象擴(kuò)展到超出其StringBuilder.MaxCapacity屬性定義的長度。
公共語言運(yùn)行時無法分配足夠的連續(xù)內(nèi)存來成功執(zhí)行操作。任何需要分配內(nèi)存的屬性分配或方法調(diào)用都可能引發(fā)此異常。
設(shè)置字符串-
string StudentName = "Tom"; string StudentSubject = "Maths";
現(xiàn)在您需要使用分配的容量進(jìn)行初始化,該容量是初始值的長度-
StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length);
現(xiàn)在,如果您嘗試插入其他值,則會發(fā)生異常。
sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1);
發(fā)生以下異常-
System.OutOfMemoryException: Out of memory
要捕獲錯誤,請嘗試以下代碼-
示例:
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ù)庫更新為上傳失敗,文件太大
if (mongoData == null)
{
apiHelper.UpdateUploadToQiniuFileTooLarge(video.EHost);
}
else
{
mongoData.UpdateUploadToQiniuFileTooLarge(video.EHost);
}
LogHelper.Log(LogFilePrefix+"uploadFileTooLarge", "文件" + fileName + "太大,大小為:" + strFileSize);
continue;
}
LogHelper.Log(LogFilePrefix + "uploadInfo", "開始上傳" + 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ù)庫
}
LogHelper.Log(LogFilePrefix + "uploadsuccess", "上傳成功,源文件名:" + video.FileName + ";新文件名:" + newFileName);
if (JudgeFileStatus(fileFullPath, fileName))
{
try
{
File.Delete(fileFullPath);
}
catch (Exception ex) { }
}
setCount++;
}
}
else
{
//把數(shù)據(jù)庫重置為要重新下載
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)部錯誤" + memoryEx.InnerException?.Message);
}
catch (Exception ex)
{
LogHelper.Log(LogFilePrefix + "uploadError", "失敗,文件名" + video.FileName + ",異常信息:" + ex.Message + ";內(nèi)部錯誤" + ex.InnerException.Message);
}
System.Threading.Thread.Sleep(5 * 1000);//休眠
}
if (setCount <= 0)
{
LogHelper.Log(LogFilePrefix + "uploadInfo", "暫無新待上傳數(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并生成以下錯誤-
輸出結(jié)果:
Error:
Global.IsOutOfMemoryException = true;
LogHelper.LogWithLock(LogFilePrefix + "uploadOutOfMemoryException", "失敗,文件名" + video.FileName + ",異常信息:" + memoryEx.Message + ";內(nèi)部錯誤" + memoryEx.InnerException?.Message);
到此這篇關(guān)于在C#中捕獲內(nèi)存不足異常的文章就介紹到這了,更多相關(guān)C#捕獲內(nèi)存異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity實(shí)現(xiàn)游戲傷害數(shù)字顯示HUD的方法
游戲中收到傷害掉血,會有飄動的傷害數(shù)值,本文主要介紹Unity實(shí)現(xiàn)游戲傷害數(shù)字顯示HUD的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
本文主要介紹了C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

