81976 05239 (admin@BangaloreJUG.org)

Home » c1 » Restricting runtime compiler on a method – CompileOnly option

Restricting runtime compiler on a method – CompileOnly option

If you know the details of runtime (JIT) compiler and you want to restrict some method to go for runtime compiler, we have a JVM arg called (-XX:CompileOnly)

Definition as per the documentation :-

Sets the list of methods (separated by commas) to which compilation should be restricted. Only the specified methods will be compiled. Specify each method with the full class name (including the packages and subpackages). For example, to compile only the length() method of the String class and the size() method of the List class, use the following:



Now, how to check that it actually restrict compilation on the method mentioned. OK, here I wrote a small snippet where I have heavily used substring method and I will restrict this method. Ideally, it should take much higher time than normal execution (which use C1 or C2 runtime compiler).

Code :-

VAICHOUD-MAC:bin Vaibhav$ cat StringExample.java

public class StringExample {

public static void main(String[] args) throws Exception {

long start = System.currentTimeMillis();

String[] string = new String[20000000];

for(int i=0;i<20000000;i++) {

string[i] = “Hello” + i;  // “ДЖАВА” + i;

String s = string[i].substring(2);


long end = System.currentTimeMillis();

System.out.println(end – start + ” ms”);





Run Results :-

VAICHOUD-MAC:bin Vaibhav$ ./java StringExample

13477 ms

VAICHOUD-MAC:bin Vaibhav$ ./java -XX:CompileOnly=java/lang/String.substring StringExample

67726 ms



Why and where to use :-

VM crashed in production and we see that crash is happened because of C1/C2 compiler and in some X method.

  • Restrict that method with JIT (C1/C2) and see if things are working fine or not. So, a kind of debugging step.
  • If customer want the application to be up ASAP and they are seeing consistent VM crash. We can tell them to restrict this method by using CompileOnly and get back into the production. So, kind of a workaround.

Leave a Reply

Your email address will not be published. Required fields are marked *