I have been testing java optimizations lately and I have found a bug in the Java compiler by accident. The JDK version I use is 8 update 112, which is the latest production release available at this point (Dec 28, 2016). I have just reported the bug to Oracle, hopefully they will fix it soon. I will explain in this blog post how to reproduce the bug. My computer is a MacBook Pro (i7 / 8Gb RAM / Early 2013 / OS X Yosemite).

EDIT: The bug is now confirmed: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8172106

Basically I wrote a small program that creates a big Java class with lots of unused assignments. Originally my intention was to check how the JVM would handle the performance of unused assignments (hopefully JIT would ignore them all, but I will check on that later), when I suddenly stumbled upon the bug.

Consider the source code below. This is part of my original code and I removed some pieces that are not needed to reproduce the bug:

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { final String folder = "/Users/hugo/temp/"; BufferedWriter out = new BufferedWriter(new FileWriter(folder + "Performance.java")); out.write("public class Performance {

"); out.write("\tpublic static int doNothing() {

"); out.write("\t\tint i = 0;

"); for (int i = 1; i < 99999999; i++) { out.write("\t\ti = " + i + ";

"); } out.close(); } }

This code compiles correctly. If you run this code (you should change the “folder” variable first and point it to a valid folder on your computer), it will generate a file called Performance.java on your computer. This is a big source file with 1.5Gb in size.

Now if you try to compile this class:

javac Performance.java

You will get the following error message:

An exception has occurred in the compiler (1.8.0_112). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you. java.lang.IllegalArgumentException at java.nio.ByteBuffer.allocate(ByteBuffer.java:334) at com.sun.tools.javac.util.BaseFileManager$ByteBufferCache.get(BaseFileManager.java:325) at com.sun.tools.javac.util.BaseFileManager.makeByteBuffer(BaseFileManager.java:294) at com.sun.tools.javac.file.RegularFileObject.getCharContent(RegularFileObject.java:114) at com.sun.tools.javac.file.RegularFileObject.getCharContent(RegularFileObject.java:53) at com.sun.tools.javac.main.JavaCompiler.readSource(JavaCompiler.java:602) at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:665) at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:950) at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:857) at com.sun.tools.javac.main.Main.compile(Main.java:523) at com.sun.tools.javac.main.Main.compile(Main.java:381) at com.sun.tools.javac.main.Main.compile(Main.java:370) at com.sun.tools.javac.main.Main.compile(Main.java:361) at com.sun.tools.javac.Main.compile(Main.java:56) at com.sun.tools.javac.Main.main(Main.java:42)

I have found bugs in the JVM before, but this is the first bug I have found on the Java compiler. You can leave your comments below.