升级到最新的JDK后,我们(在某些机器上)有一个奇怪的OutOfMemoryException
。
考虑这个简单的应用程序:
public class Test
{
public static void main (String[] args) {
try {
java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("E dd/MM/yyyy HH:mm");
System.out.println("formatted date: " + dateFormatter.format(new java.util.Date()));
} catch (Exception x) {
System.err.println(x);
System.exit(1);
}
}
}
运行这个小程序会导致此异常(即使使用-Xmx2048M -Xms2048
运行它):
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Currency.readLongArray(Currency.java:657)
at java.util.Currency.access$100(Currency.java:76)
at java.util.Currency$1.run(Currency.java:211)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.Currency.<clinit>(Currency.java:192)
at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:566)
at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:94)
at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:157)
at java.text.NumberFormat.getInstance(NumberFormat.java:767)
at java.text.NumberFormat.getIntegerInstance(NumberFormat.java:439)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:664)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
at Test.main(Test.java:5)
有人可以向我解释一下吗?
我们使用的java版本是:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
当我们使用先前版本时,我们没有问题:
java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b16)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java Test
formatted date: ma 21/10/2013 10:19
更新:在每个人都提到增加堆大小之前......我已经尝试过了:
java -Xmx2048M -Xms2048M测试
Runtime.getRuntime().freeMemory(): 2048120480
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Currency.readLongArray(Currency.java:657)
at java.util.Currency.access$100(Currency.java:76)
at java.util.Currency$1.run(Currency.java:211)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.Currency.<clinit>(Currency.java:192)
at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:566)
at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:94)
at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:157)
at java.text.NumberFormat.getInstance(NumberFormat.java:767)
at java.text.NumberFormat.getIntegerInstance(NumberFormat.java:439)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:664)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
at Test.main(Test.java:8)
答案 0 :(得分:10)
我挖掘了java.util.Currency
的代码。它在静态类初始化程序块中读取位于Java JRE中的资源文件。
String homeDir = System.getProperty("java.home");
try {
String dataFile = homeDir + File.separator +
"lib" + File.separator + "currency.data";
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(dataFile)));
if (dis.readInt() != MAGIC_NUMBER) {
throw new InternalError("Currency data is possibly corrupted");
}
formatVersion = dis.readInt();
if (formatVersion != VALID_FORMAT_VERSION) {
throw new InternalError("Currency data format is incorrect");
}
dataVersion = dis.readInt();
mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
int scCount = dis.readInt();
scCutOverTimes = readLongArray(dis, scCount);
如您所见,它读入 JRE / lib / currency.data 。第四个整数包含scCount
。这个整数太高了。我猜那个文件已损坏了。尝试更换它。