java堆空间-bytearrayoutputstream.write

6ioyuze2  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(390)

写了这个程序来解压和解压缩文件到amazons3。我遇到了一个java堆错误。
我尝试过的方法:增加参数的堆空间。将字节大小更改为[1024*1024]
在outputstream.write(buffer,0,len)处获取错误。字节大小初始化为1024。到目前为止,这段代码适用于最大166MB的大多数文件。java堆大小为xmx4096mJava版本1.7
解压方法:

public static void extractObjects(byte[] buffer, AmazonS3 s3Client, ZipInputStream zis, ZipEntry entry)
                throws IOException {
            try {
                while (entry != null) {
                    String fileName = entry.getName();
                    if (fileName == "lib") {
                        fileName = entry.getName();
                    }
                    boolean containsBackup = fileName.contains(doc.getDesiredFile());

                    if (containsBackup == true) {
                        System.out.println("A back up file was found");
                        formatSchemaName();
                        System.out.println("Extracting :" + app.getCurrentPacsId());
                        log.info("Extracting " + app.getCurrentPacsId() + ", compressed: " + entry.getCompressedSize() + " bytes, extracted: " + entry.getSize() + " bytes");
                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                        int len;

while ((len = zis.read(buffer)) >= 0) 
                        {
                            outputStream.write(buffer, 0, len);
                        }
                        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                        meta = new ObjectMetadata();
                        meta.setContentLength(outputStream.size());
                        fileName = app.getCurrentPacsId();
                        runDataConversion(is,s3Client,fileName);

                        is.close();
                        outputStream.close();
                        System.out.println("Unzip complete");               
                    }
                    else{
                        System.out.println("No back up found");
                    }
                    entry = zis.getNextEntry();
                }
                zis.closeEntry();
                zis.close();
            } catch (AmazonServiceException e) {
                log.error(e);
            } catch (SdkClientException e) {
                log.error(e);
            }
        }

错误

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2271)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
    at com.amazonaws.image.DataMiner.extractObjects(DataMiner.java:112)
    at com.amazonaws.image.DataMiner.downloadBucket(DataMiner.java:76)
    at com.amazonaws.image.DataMiner.obtainConnection(DataMiner.java:58)
    at com.amazonaws.image.DataMiner.main(DataMiner.java:208)
jljoyd4f

jljoyd4f1#

你真的需要这个吗 ByteArrayOutputStream ? . 看起来你只是用它来获得未压缩的大小,但你已经有了它 entry.getSize() . 你能把钥匙递给我吗 ZipInputStream 直接到 runDataConversion(...) ?
至于您观察到的实际问题,当达到这些内存消耗级别时,遇到碎片问题并不罕见。也就是说,虽然您有比请求的更多的可用内存,但是您没有一个大的连续块,因此分配失败。一个压缩垃圾收集器应该处理这个问题,但并不是jvm中的所有垃圾收集器都在压缩iirc。

相关问题