How to speed up Java applications? - java

I need to use Java for a desktop application. I heard that there are many tools that compile java natively, is this true? does these tools compile java program into machine code?
THank you!

Since the (Sun/Oracle) Java VM has a good JIT (just-in-time) compiler, you don't have to compile your Java program to machine code yourself. The compiler will do that on the fly when it's necessary.
So: Speed up your Java programs just as every other program:
reduce algorithmic complexity
exploit parallelism
compute at the right moment
find and remove bottlenecks
...
Since Java is a garbage collected language, there is one important point to more speed: reduce allocations! Reducing allocations will help you at least twice: The allocation itself isn't done and the garbage collector will have to do less work (which will save time).

I agree with the others that compiling to machine code does not make much sense: mind that C free/malloc have same or higher costs than Java new/garbage collection.
The NetBeans IDE comes with a built-in Profiler; so you could profile your application in that IDE to find bottlenecks.

are you coding the app or it's someone's else?
It looks you're trying to run an java app that is slow. Try increasing the memory when running it. You can change the shell script specifying these params:
java -Xms64m -Xmx512m

I need to use Java for a desktop application. I heard that there are
many tools that compile java natively, is this true? does these tools
compile java program into machine code?
Such programs do exist, but may come with tradeoffs when using some of the more dynamic capabilities of the Java platform like you may lose the ability to load new classes at runtime. The JVM may have a slow start up, but it's plenty fast enough once it gets going.
That said, one solution that I didn't see anyone mention here is to replace code written in Swing with SWT. The SWT toolkit uses native code underneath.

Related

Java program and JVM produced exe

Java is platform independent , because it use a platform dependent JVM to launch a Java program. JVM understands bytes codes and executes program. I know old way of doing this was interpreter. But now JVM's is using JIT. But I didn't understand JIT concept clearly. I think, a jVM can translate byte codes to an exe (for Windows) and after this I can run this translated program without JVM. But I can see generated exe in .net JIT but I cannot see generated exe in Java.
How can I do this (create a native exe file from Java)?
What will be performance of JVM produced exe vs the same C application?
How Java will handle static linking and dynamic linking?
Some thoughts:
You can't generate an exe using just core Java, and the JVM certainly doesn't do this. It still runs the byte code in the JVM. The actual exe that runs here is java.exe.
It's hard to say if an exe would be significantly faster since the JIT generates pretty darn fast code.
Java doesn't create dll's if that's what you mean, but it can interact with well-formed dll's (not .Net dll's) via JNI and JNA. If you wanted to interact with .Net libraries, I think you would need to go another route such as sockets or COM interface.
Also as an aside, there do exists programs that create exe's from java classes, but most create files that still require a JVM, and the good ones that don't I believe are not free.
Does not make much sense to do so.
Sometimes faster, sometimes slower.
No idea.
I think, a jVM can translate byte codes to an exe (for Windows) and after this I can run this translated program without JVM.
This is not correct.
In reality the Hotspot JIT compilers work by compiling individual methods to native code within the running JRE. They typically only bother to compile methods after they have been called a few times to gather stats on typical execution paths. No "exe" is ever produced by the Hotspot JIT compilers.
1) How can I do this (create a native exe file from Java)?
There are third party applications that will do this. However, by doing this you lose a lot of the advantages of JIT compilation, such as optimizing for the execution patterns of the current program run.
2) What will be performance of JVM produced exe vs the same C application?
That depends on the application.
3) How Java will handle static linking and dynamic linking?
Java doesn't handle this. It depends on the 3rd party application that you use to create the executable.
I would recommend not going down this path. If it is critical that you distribute your code as ".exe" files, you probably shouldn't be using Java.
How can I do this (create a native exe file from Java)?
There is a common mis conception that this will be better in some way. However, I haven't seen a good situation where it is. There are products like Excelsior JET which can compile a binary. GCC can compile a binary for Java 1.4 but this is not a current project AFAIK.
What will be performance of JVM produced exe vs the same C application?
You can't compare the two. If you want an Object Orientated Program, you can't easily write this in C and it will almost certainly be much slower to write if not run. If you want to write a C style program, write it in C.
How Java will handle static linking and dynamic linking?
The JVM does dynamic late linking. If fact it can link and re-link code multiple times as you load and unload class loaders.

Java JIT compiler for download

I want to download a JIT compiler for Java. Where can I get a good JIT compiler?
The JIT compiler runs as part as the JVM - it's not something you download and run separately.
In the early days of Java, you used to be able to run a JIT as a plugin - but these days there's no need. Any mainstream, modern desktop Java environment will include a JIT.
Most of the JVMs have a JIT built in. Download any of them.
My guess is you are looking for a java to native .exe compiler in the mistaken belief that this will yield a significant performance difference. For Java this is not the case for most real applications and just makes deploying them harder.
Any modern well-performing Java implementation comes with a JIT, and normally you do not have to worry about these kind of things. The most frequent is the Oracle Java implementation available from http://java.com.
If you, however, have a performance problem it is usually a problem with your own code, so use a suitable profiler (jvisualvm in the Sun 6 JDK is a good, free starting point) to identify your bottlenecks, so you can correct them.

Achieving multi-core in Java - how?

What is the easiest way in Java for achieving multi-core? And by this, I mean, to specifically point out on what core to execute some parts of the project, so good-old "normal" java threads are not an option.
So far, I was suggested JConqurr (which is an Eclipse toolkit for multi-core programming in java), JaMP (which extends Java for OpenMP), and MPJ express, of which I don't know much. Which of the above do you consider the best, or do you have other suggestions? It would be preferable to somehow mesure the performance boost/gain, but not exclusive.
Any help would be much appreciated.
Thanks,
twentynine.
Even though it is easy to write multi-threaded code in Java, there is nothing in the Java standard runtime which generically allow you to tell the JVM or the operating system how to schedule your program.
Hence you will need to have code specifically for your JVM and/or your operating system, and that code may not be doable in Java (unless you dive into JNI or JNA). External programs can pin processes to a CPU in many Unix versions (and probably Windows too), but I don't think you can do this for individual threads.
Scala is quite popular for this. It runs on the JVM and has bindings for Java to hook them together.

i want a java editor with embedded compiler and work under linux and did not require a a big amount of ram

I want a Java IDE with a compiler for Linux and that does not require a a big amount of RAM.
I have 256 MB of ram, so my options are limited
In my first programming courses, we used BlueJ as our "IDE". Really basic, yet gets the job done. It honestly don't have a lot of features, but it's lightweight, portable and effective
You have to link it with an external compiler though, but it shouldn't be a problem if you already develop in Java with this computer.
Eclipse would be the last thing you want. I can gather you've probably already tried it from the title. For a small footprint I'd suggest JEdit. It's much smaller, mature and extensible. There's also Kate if you run KDE. While it doesn't have the embedded compiler support you could add one in as an external command.
What do you mean by "IDE".
You could use Emacs with that kind of memory foootprint, use Ant for the builds and emacs would allow you to debug in the editor, and also take you to compile errors...
But it would not have a GUI development component.
For more detail on using Emacs as a full featured Java IDE, read this IBM document:
http://www.ibm.com/developerworks/java/library/j-emacs/
Does the 256MB of RAM also include program space, or will the code be stored elsewhere in flash or another memory type?
You can get embeddable java machines, and small java compilers for linux. The IDE, however, is tricky as you don't merely say editor, but you want a whole integrated environment.
Since you have Java installed already, I suggest Eclipse. It seems bulky and resource intensive on PCs, but you can cut it back significantly by removing features (classes) you don't want/need.
-Adam
jEdit will work easily.
Eclipse 1.x will work in that amount of RAM. Depending on your version of java, Eclipse 1.x will definitely work in that RAM.

What can you not do on the Dalvik VM (Android's VM) that you can in Sun VM?

I know that you can run almost all Java in Dalvik's VM that you can in Java's VM but the limitations are not very clear. Has anyone run into any major stumbling blocks? Any major libraries having trouble? Any languages that compile to Java byte code (Scala, Jython etc...) not work as expected?
There is a number of things that Dalvik will not handle or will not handle quite the same way as standard Java bytecode, though most of them are quite advanced.
The most severe example is runtime bytecode generation and custom class loading. Let's say you would like to create some bytecode and then use classloader to load it for you, if that trick works on your normal machine, it is guaranteed to not work on Dalvik, unless you change your bytecode generation.
That prevents you from using certain dependency injection frameworks, most known example being Google Guice (though I am sure some people work on that). On the other hand AspectJ should work as it uses bytecode instrumentation as a compilation step (though I don't know if anyone tried).
As to other jvm languages -- anything that in the end compiles to standard bytecode and does not use bytecode instrumentation at runtime can be converted to Dalvik and should work. I know people did run Jython on Android and it worked ok.
Other thing to be aware of is that there is no just in time compilation. This is not strictly Dalviks problem (you can always compile any bytecode on the fly if you wish) but that Android does not support that and is unlikely to do so. In the effect while microbenchmarking for standard Java was useless -- components had different runtime characterstics in tests than as parts of larger systems -- microbenchmarks for Android phones totally make sense.
If you see "Dalvik Virtual Machine internals" Google IO session, you can find Dalvik does not support generational GC.
So, it could degrade performance of frequent object creation and deletion. Java VM supports generational GC so, it would show better GC performance for the same situation.
And also, Dalvik uses trace-granuality JIT instead of method granuality JIT.
Another thing that I guess could be added here is that Dalvik apparently does not preserve field order when listing the fields of a class using the reflection API. Now, the reflection API does not make any guarantees on it anyway (so ideally you shouldn't depend on it anyway), but most of the other VMs out there do preserve the order.
Just to add to the conversation, not intended to revive an old thread. I just ran across this in my search, and want to add that Jython does not work out of the box with Dalvik either. Simply trying to do a hello world example will yield the following:

Categories

Resources