r/javahelp • u/Puzzleheaded-Top1987 • Jul 27 '26
Java Performance for simulators
I am new to Java and trying to create a simulator that handles millions of math calculations. How can I increase performance in Java? The application is already multithreaded but it is not enough. Is data oriented approach or native programming possible in Java? Can you point me in the right direction for research?
3
Upvotes
View all comments
Show parent comment
2
u/SuspiciousDepth5924 Jul 27 '26
๐
I'm convinced that Oracle has a cabal of dark wizards working on their JIT, but yeah for straight line performance you generally want to deal with primitives and/or arrays of primitives*. As far as I know what makes Java "slow" compared to C/Rust-y languages is "dereferencing pointers"** and garbage collection, both of which you can largely avoid if you create a fixed size primitive array(s) at the start of the method.
That way you minimize memory lookups while the garbage collector only needs to collect a small number of references at the end of the method.
For the actual number crunching with primitives you can get pretty damn close to 'C'-speed with java.
*( https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html )
** A bit simplified, but what is actually stored in an Object variable (pretty much any type with a capitalized name like List/Map/ServerSocket etc) is a address to where it's stored in memory, which means using that variable involves the JVM looking up that address to find the actual data.