Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryException (after upgrade to JDK 7u45)

After upgrading to the latest JDK, we've got (on some machines) a strange OutOfMemoryException.

Consider this simple application:

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);
        }
    }
}

Running this small program will result in this exception (even when running it with -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)

Could someone please explain this to me?

The java version we are using is:

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)

When we are using a prior version, we have no problem:

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

Update: Before everyone is mentioning increasing the heap size... I already tried it:

java -Xmx2048M -Xms2048M Test

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)
like image 321
Joris Avatar asked Oct 21 '13 08:10

Joris


1 Answers

I dug into the code of java.util.Currency. It reads in a resource file located in the Java JRE, in the static Class initializer block.

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);

As you can see, it reads in JRE/lib/currency.data. The fourth integer contains the scCount. This integer will be too high. I guess that that file is corrupt. Try replacing it.

like image 129
Martijn Courteaux Avatar answered Oct 26 '22 23:10

Martijn Courteaux