文档的java.lang.Error说:
Error是Throwable的子类,表示合理的应用程序不应该尝试捕获的严重问题
但是作为java.lang.Error子类java.lang.Throwable,我可以捕获这种类型的Throwable.
我明白为什么抓住这种例外不是一个好主意.据我所知,如果我们决定捕获它,catch处理程序不应该自己分配任何内存.否则OutOfMemoryError将再次抛出.
所以,我的问题是:
java.lang.OutOfMemoryError可能是一个好主意?java.lang.OutOfMemoryError,我们怎么能确定catch处理程序本身不分配任何内存(任何工具或最佳实践)?我必须从许多图像中导入大量的图像裁剪,这些图像都已准备好存储在我的数据库中.我每次尝试使用语句并处理我的位图对象.但我仍然得到一个内存溢出异常,我的系统内存不足.
以下是我正在做的一些示例代码.
public void CropImage(List<ImageClass> data)
{
foreach (var obj in data)
{
//I have a data base method that returns a data object that
//contains the file bytes of the image id in data: 'file'
//My List<ImageClass> data contains an ID of the original image
//start x,y coords for the upper left corner of the rectangle,
//and the width and height of the rectangle.
Image img = Image.FromStream(new MemoryStream(file.Data));
Bitmap bmp = new Bitmap((Bitmap)img);
Rectangle cropArea = new Rectangle(obj.x_coordinate, …Run Code Online (Sandbox Code Playgroud)