user1472672
user1472672

Reputation: 365

Why does Files.copy cause out of memory error when creating large file

I have a simple method that creates a tar file with a collection of compressed files inside:

private byte[] gzipCompress() throws IOException {
    File source = super.getPath();
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (GZIPOutputStream gos = new GZIPOutputStream(baos)) {
        Files.copy(source.toPath(), gos);
    }
    
    return baos.toByteArray();
}

When the file is large (not sure of exact size 1GB maybe) - this method causes an error:

Caused by: java.lang.OutOfMemoryError: Java heap space

Seems similar to this question - java nio Files.copy thowing Java heap space out of memory, but is there some other alternative to the IOUtils.copyLarge solution.

Upvotes: 0

Views: 677

Answers (0)

Related Questions