欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读写文件时内存溢出问题思考(OutOfMemoryError: Java heap space)

程序员文章站 2022-07-13 13:16:37
...

回忆一下:1年前在做断点续传因粗心编码导致的内存溢出问题。当时在做分片时,当分片容量大于512M时,内存溢出,抛出异常:java.lang.OutOfMemoryError: Java heap space。

分析一下:当时虚拟机堆内存正好设置为512M,当申请堆空间大于该值时出现如上异常。

今日重写代码测试,讲解出错的原因以及解决方案。

public static void testOutMemory() throws IOException {
		//绝对路径
		String inPath = "D:/TestFile/in/bigFileTest.zip";
		String outPath = "D:/TestFile/out/bigFileTest.zip";
		
		File inFile = new File(inPath);
		File outFile = new File(outPath);
		if (!inFile.exists()) {
			//TODO 文件不存在
		}
		long fileLength = inFile.length();//文件大小,我的测试值为: 1820595407
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) fileLength);//这一行会抛出异常
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));
		FileOutputStream fileOutputStream = new FileOutputStream(outFile);
			
		int buf_size = 1024;
		byte[] buffer = new byte[buf_size];
		int len = 0;
		while (-1 != (len = in.read(buffer, 0, buf_size))) {
			bos.write(buffer, 0, len);
		}
			
		byte[] data = bos.toByteArray();
		fileOutputStream.write(data);
		fileOutputStream.flush();
		fileOutputStream.close();
        bos.close();
		System.out.println("测试完成");
	}

执行方法,在ByteArrayOutputStream bos = new ByteArrayOutputStream((int) fileLength);这一行会抛出异常,查看ByteArrayOutputStream构造方法,其中有一句为buf = new byte[size], 也正是在这一句抛出的异常,问题已经找到。

难道需要通过增加堆内存的方式来解决这个问题?当然不是。虽然可以暂时解决问题,但依然会埋下一枚地雷,当下一文件更大时,这个问题将会重新出现。

废话不多说,在代码中用一次缓存较大数据本身就不可取,上面的代码实现的文件传输本就不应该先把文件存放到内存,再将内存中的数据传输到其他位置,很明显这是一次很垃圾的编码。于是,优化如下:

public void testOutMemory() throws IOException {
		// 绝对路径
		String inPath = "D:/TestFile/in/bigFileTest.zip";
		String outPath = "D:/TestFile/out/bigFileTest.zip";

		File inFile = new File(inPath);
		File outFile = new File(outPath);
		if (!inFile.exists()) {
			// TODO 文件不存在
		}
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));
		FileOutputStream fileOutputStream = new FileOutputStream(outFile);

		int buf_size = 1024;
		byte[] buffer = new byte[buf_size];
		int len = 0;
		while (-1 != (len = in.read(buffer, 0, buf_size))) {
			fileOutputStream.write(buffer, 0, len);//一次仅传输1K,不会溢出
		}

		fileOutputStream.flush();
		fileOutputStream.close();
		System.out.println("测试完成");
	}

(PS:我的堆内存为2G,文件大小约1.7G,为什么会内存溢出了,在这篇文章中会进行详细探索)。