繁体   English   中英

编写巨大的文件和Java堆空间

[英]writing a huge file and Java heap space

我正在尝试在磁盘中写入500mb至1.5 gb之间的巨大文件。 我用zipOutputStream压缩它,然后通过网络发送

但是在客户端中,当我尝试解压缩并编写它时,它给了我Java heap space. 例外

  try (FileOutputStream fos = new FileOutputStream(outFile)) {
                int fileLength = (int)zipEntry.getSize();

                byte[] fileByte = new byte[fileLength];
                zips.read(fileByte);
                fos.write(fileByte);
            }

我知道那是一个字节数组相当大的内存分配,但是我怎么可能解决呢?

您正在创建的byte[]数组是您的缓冲区,当从InputStreamOutputStream传输时,缓冲区用作堆中的临时位置。 除非您希望程序在内存中使用500mb-1.5gb,否则需要减小缓冲区大小。 这是我执行此操作的常用方法。 此方法使用一个1kb的缓冲区,您可以播放该大小并查看最适合您的大小。

/**
 * writes all bytes from inputStream to outputStream
 * @param source
 * @param destination
 * @throws IOException
 */
public static void pipeStreams(java.io.InputStream source, java.io.OutputStream destination) throws IOException {

    // 1kb buffer
    byte [] buffer = new byte[1024];
    int read = 0;
    while((read=source.read(buffer)) != -1) {
        destination.write(buffer, 0, read);
    }
    destination.flush();
}

使用此方法的代码看起来像

try (FileOutputStream fos = new FileOutputStream(outFile)) {
    pipeStreams(zips, fos);
}

您可以使用Files.copy(zips, outFile.toPath());完成相同的操作,而不必一次将整个文件都读入内存Files.copy(zips, outFile.toPath()); 这里是Files.copy方法的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM