Posts Tagged ‘overhead’

Measuring the Overhead of a JNI Call

Thursday, February 10th, 2011

I have been doing a fair amount of work with Java lately, and a lot of it has required me to make system calls to the Linux kernel, via JNI. I got to wondering what the actual overhead of a JNI call is versus the time it takes to execute the C code.

So, I wrote a simple test application ( to test it out for myself.

This application makes two calls to JNI. Each call measures the time stamp counter. In doing so, the difference between the first and second calls would be the overhead of one call (the tail end of the first call, and the first half of the second). I then make the same calls through C, and compare the difference. I then repeat this 1,000,000 times in both Java and C.

I found that on my desktop PC (Core 2 Duo 4400 @2.00GHz, 2GB RAM, Ubuntu 10.04, OpenJDK 1.6, gcc 4.4.3) the difference seems to hover around 150 clock ticks. Making the function calls in C is usually about 275 ticks, and Java is close to 430. Here are the results from a sample run of my code:

Average time in C: 276.22658 ticks (stddev: 641.930900322, min: 270, max: 251470)
Average time in Java: 427.31173 ticks (stddev: 1073.98186333, min: 370, max: 286170)

While JNI obviously isn’t going to be as speedy as raw C, 150 clock cycles isn’t horribly slow either.