views:

285

answers:

5

Some modern network cards support Direct Memory Access for improved performance. How can I utilize this feature from Java?

Does the JVM provide this automatically, or do I need to do an allocateDirect on the ByteBuffers that I am using to talk to that NIC?

Does anyone have documentation that discusses this?

+2  A: 

It is the operating systems task to use the DMA feature of the network card. The JVM does not really care how the OS does it, and simply uses the operating system's functions for talking to "network interfaces".

ankon
A: 

Yeah - ankon's answer is right. Java operates in a sandbox - a virtual machine (hence the, "VM" in JVM; Sun actually built ONE physical version -- it's on display somewhere).
Java was never designed (intentionally) to reach outside the sandbox, unlike ActiveX, which can go just about anywhere on a PC.

Just think of all the bad things ActiveX has done over the years via a browser. You wouldn't want that to happen with Java, would you?

inked
I think you're getting slightly confused between Java Applets (run in a sandbox in a browser) and Java Applications (have no security restrictions).
Pool
The sandbox model only applies to applets. A Java application can potentially cause as many damage as any other. For example deleting your system files. But it's true, unless you use JNI, you don't have direct access to the hardware
jassuncao
A: 

Although...

you might be able to instantiate an object in Java that does have access to the hardware (like one of those ActiveX controls, or some DLL, for example - which you'd have to write, too).

The problem I see is the throughput. With 100MB or 1000MB cards, would a JVM (remember, this is a VM running on an OS, so you're a couple of layers removed from the hardware) have the speed to handle what's coming in under load? Would you want a Java program holding up data in your NIC while it tinkered with it (think of the impact to the rest of the system)?

At this point, you're probably better off writing the hard-working guts of your solution in C. And, if you still need Java to play with that data, put it in a place where Java can get to it.

inked
+1  A: 

You cannot do this from inside Java in the typical desktop/server JVMs, as this is operating system area which requires you to reach out into C code. Go have a look on JNI or JNA to see how to do this. Please note that this may make your application brittle if you do not get this exactly right.

Thorbjørn Ravn Andersen
A: 

If you're not getting the network throughput you need in java, then you're going to need to write a C wrapper in order to access it.

Have you benchmarked your code to find where your performance issues really are? If you let us know that we can likely help you out without resorting to JNI.

johnny