Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, April 14, 2011

What is JIT

The Just-In-Time (JIT) compiler is a part of JVM to improve application performance.

As we all know the class files generated with javac contain platform-independent bytecodes to be interpreted by a JVM on specific OS/machine.  Due to this interpretation performance will be slow than that of a native application.  The JIT compiler compiles bytecodes into native machine code at run time and helps in improving the performance.  Below are some points to be noted:
  • It is enabled by default, and is activated when a Java method is called.
  • It compiles the bytecodes of that method into native machine code, compiling it "just in time" to run.
  • After a method is compiled, JVM calls the compiled code directly instead of interpreting it.
  • Methods are not compiled the first time they are called.  For each method, JVM maintains a call counter.  JVM interprets a method until its call count exceeds a JIT compilation threshold.
  • After a method is compiled, its call count is reset to zero and subsequent calls to the method will continue to increment its count.  When the call count of a method reaches the JIT recompilation threshold, the JIT compiler compiles it a second time, applying further optimizations than on the previous compilation.  This process is repeated until the maximum optimization level is reached.
  • The JIT compiler can be disabled (-Xint interpret-only mode), in which case the entire Java program will be interpreted.


Monday, December 7, 2009

Starting java without command prompt

I have a jar file (self-executable, with Main-class attribute set) and to run it I just need to double click it as jar files are associated with javaw on my machine.  However, I needed to pass some arguments to this jar.  So I put this one line into a cmd file and tried to run:

javaw -jar myJar.jar -Duser.home=C:\myTest\myHome

When I run this even though my GUI (jar is a swing app) comes up, command window doesn't go away.  One has to put "start" also before for the command window to go away.  So the above line should read as below:

start javaw -jar myJar.jar -Duser.home=C:\myTest\myHome

By the way, we can have all the profile files to be created at mentioned folder instead of default user.home by overriding the system variable as above.

Saturday, July 12, 2008

DecimalFormat issue in jdk 1.4

Any message handling application need to maintain an unique id for each message to track the status. We are generating ids in Oracle database, which are 24 chars long. As this is done with a sequence the id we get is having some zeros followed by a number like '000000001234567890123456'. To remove the leading zeros we are simply converting this to BigDecimal and then again back to string using DecimalFormat as shown in the below code. While doing this we realised that the max number that DecimalFormat can format successfully is 15 digits if all 9's else 16 digits. Any value beyond that is causing the number to be incremented as shown in the below output.

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.StringTokenizer;

public class Test
{
public static void main(String[] args)
{
String[] data = {"999999999999999", "9999999999999998", "9999999999999999", "18411028910519619"};
BigDecimal num = null;
DecimalFormat format = new DecimalFormat();

for(int i = 0;i < data.length; i++)
{
num = new BigDecimal(data[i]);
System.out.println("Bignum: " + num.toString() + " converted to: " + format.format(num));
}
}
}

Output with JDK 1.4:
Bignum: 999999999999999 converted to: 999,999,999,999,999
Bignum: 9999999999999998 converted to: 9,999,999,999,999,998
Bignum: 9999999999999999 converted to: 10,000,000,000,000,000
Bignum: 18411028910519619 converted to: 18,411,028,910,519,620

This is not an issue in JDK 1.6:
Bignum: 999999999999999 converted to: 999,999,999,999,999
Bignum: 9999999999999998 converted to: 9,999,999,999,999,998
Bignum: 9999999999999999 converted to: 9,999,999,999,999,999
Bignum: 18411028910519619 converted to: 18,411,028,910,519,619