gpt4 book ai didi

java - 尝试使用 ZipFileSet 提取大 jar 时出现 OutOfMemory 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:19 25 4
gpt4 key购买 nike

使用 jdk1.5,我在尝试提取相当大的 jar 时遇到 OutofMemoryError。但是,这不会发生在 jdk6 上。是因为 jdk1.5 和 jdk6 上的默认堆大小/permgen 设置不同,还是因为 jdk1.5 中的错误已在 jdk6 中修复?

import java.io.*;
import java.util.zip.*;

public class UnZip {
final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(argv[0]);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new FileOutputStream(entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

最佳答案

引用Total Heap SizeTuning Garbage Collection with the 5.0 Java[tm] Virtual Machine :

Total Heap

(...)

By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range. This target range is set as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and -XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms and above by -Xmx . The default parameters for the 32-bit Solaris Operating System (SPARC Platform Edition) are shown in this table:

-XX:MinHeapFreeRatio= 40
-XX:MaxHeapFreeRatio= 70
-Xms 3670k
-Xmx 64m

Default values of heap size parameters on 64-bit systems have been scaled up by approximately 30%. This increase is meant to compensate for the larger size of objects on a 64-bit system.

并且在 Default Heap SizeJava SE 6 HotSpot Virtual Machine Garbage Collection Tuning ,他们写道:

Default Heap Size

If not otherwise set on the command line, the initial and maximum heap sizes are calculated based on the amount of memory on the machine. The proportion of memory to use for the heap is controlled by the command line options DefaultInitialRAMFraction and DefaultMaxRAMFraction, as shown in the table below. (In the table, memory represents the amount of memory on the machine.)

                                             Formula  Default
initial heap size memory / DefaultInitialRAMFraction memory / 64
maximum heap size MIN(memory / DefaultMaxRAMFraction, 1GB) MIN(memory / 4, 1GB)

Note that the default maximum heap size will not exceed 1GB, regardless of how much memory is installed on the machine.

所以,是的,Java 6 具有非常不同的堆设置,并且堆可以增长到 RAM 的 1/4(如果你有超过 4GB,则为 1GB)即现在很可能超过 64m。

关于java - 尝试使用 ZipFileSet 提取大 jar 时出现 OutOfMemory 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2257620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com