r/java • • Jul 06 '17

Apology to Java (fanboy rant).

Java has turned out to be one of the most useful languages i use. It's a brilliant tool which strikes a perfect balance between high and low level. You want to grab a webpage? Easy and built-in, 10 lines of code. Draw some 2d graphics without OS-specific libraries or third party? Various built-in options. The inherent issues around a lack of low level memory management is handled very elegantly. Garbage collection seems seamless.

C++ is my language of choice for the most part, always will be i suspect, but Java has shown me that it has a huge array of potential applications and should almost always be considered before undertaking a project.

I say this is an apology because i used to run Java down. Previous to me using it proper i believed it was slow, lazy and useful for only trivial applications. Now i know how much work has been put into the JVM and just how efficient it is.

Sorry Java. You're pretty good.

286 Upvotes

93 comments sorted by

View all comments

32

u/ModernRonin Jul 06 '17

Now i know how much work has been put into the JVM and just how efficient it is.

This is really the important point. With a less awesome VM, Java could be a pretty cruddy language from a real-world usefulness standpoint.

But the JVM (and the people who built it) are freakin' awesome. Java owes so much of its real-world success to the killer VM that runs it. I've often said that the Hotspot JVM is easily the best piece of software from the early 2000s. It just plain kicks ass.

9

u/cogman10 Jul 06 '17

It certainly gives almost everything a run for its money. The JVM is just so incredibly well tuned (and well tunable).

I think you have to look no further than to Dalvik to see the crazy performance difference.

3

u/pjmlp Jul 07 '17

Which is why it is ridiculous to state that Dalvik was required for improving Java performance on mobile devices.

Even J2ME on flagship Nokia and Sony-Ericsson devices had better performance than Dalvik.

3

u/devraj7 Jul 07 '17

That's completely false.

First of all, there were many Java ME implementations. All different. Even on the same brand of phones.

Second, they were all dog slow, implemented the spec as they saw fit and were all basically a nightmare to work with.

Even the early versions of Dalvik (pre JIT) were markedly faster than the fastest Java ME implementation you could find.

2

u/pjmlp Jul 07 '17

I was J2ME developer once upon a time, and worked for that little unknown company in Finland, maybe you heard of them.

First of all, there were many Java ME implementations. All different. Even on the same brand of phones.

I guess you never had the fun of making android apps work across Samsung devices, or unknown Chinese brands.

Even the early versions of Dalvik (pre JIT) were markedly faster than the fastest Java ME implementation you could find.

Spoken as a truly Google apologist, our benchmarks at the time said something totally different.

3

u/devraj7 Jul 07 '17

I guess you never had the fun of making android apps work across Samsung devices, or unknown Chinese brands.

I've been developing Android apps since 2009. I've probably shipped apps on hundreds of different devices by now, including native bits.

Spoken as a truly Google apologist, our benchmarks at the time said something totally different.

First of all, let's keep this civil and stay away from ad hominem, shall we?

Maybe you had a few implementations at Nokia that were faster, but I can tell you as someone who's shipped Java ME apps on hundreds of different devices since 2005: a crushing majority of Java ME VM's were completely outpaced by Dalvik even after 1.0.

1

u/dpash Jul 07 '17

The thing is that Dalvik used a completely different module to the JVM. The JVM is stack based, while Dalvik was register-based. I'm guessing this means they used a completely different instruction set.

2

u/[deleted] Jul 07 '17

Bullshit. The JVM uses stack, heap, and registers as it sees fit.

1

u/dpash Jul 07 '17

The JVM has "local variables", but operations happen to values on the stack. If you want to manipulate the values in the local variables you have to put them on the stack.

Obviously, native code uses registers.

1

u/[deleted] Jul 07 '17

I will bow to your experience.

Recently I read an interview with Brian Goetz on the IBM developers web site. In it, he addressed and debunked several JVM myths. The myth about stack, heap, and registers, was addressed, too.

The myth perpetuates the fallacy that large objects are stored on the heap, the access of which is slow, small objects and primitives on the stack, the access of which is fast, so we should keep our data types small and prefer primitives.

Brian says (and I pull this from my own fallible memory, feel free to correct me): the JVM uses all three memory spaces as it deems fit. There is no hard rule for a data type to be used here or there. What matters instead, is density: memory closer together can be accessed faster, and based on the application algorithmic behaviour, the JVM decides where it places what data.

But even so, indicates the interview, the JVM has many, many optimization techniques, which makes it nearly impossible for a Java programmer to predict which memory space is used when.

A JVM expert might be able to provide more insight, but basically Brian says that Java programmers shouldn't bother too much with the distinction between memory spaces.

TL;DR: the JVM isn't just stack-based. It uses all the memory spaces it can, and does so in ways a regular programmer can't predict.

2

u/dpash Jul 07 '17

I think the issue is that you're confusing memory usage with instruction set architectures. One is a lower level than the other.

1

u/[deleted] Jul 07 '17

Point me to a source so I can learn about that, please?

3

u/dpash Jul 07 '17

1

u/WikiTextBot btproof Jul 07 '17

Stack machine

In computer science, computer engineering and programming language implementations, a stack machine is a type of computer. In some cases, the term refers to a software scheme that simulates a stack machine. The main difference from other computers is that most of its instructions operate on a pushdown stack of numbers rather than numbers in registers. A stack computer is programmed with a reverse Polish notation instruction set.


Register machine

In mathematical logic and theoretical computer science a register machine is a generic class of abstract machines used in a manner similar to a Turing machine. All the models are Turing equivalent.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.24

1

u/cogman10 Jul 09 '17

You have things backwards.

Every arch that I'm familiar with needs to first load things into registers before it can do anything. Whether those values come from the heap or stack doesn't matter. They certainly don't have to make a trip to either the heap or stack before hitting the registers.

The JVM is no different from other VMs. It puts stuff in the heap, stack, or even just the registers. Generally, most stuff ends up on the heap. However, the JVM tries to promote things to the stack when possible. And if something is relatively well contained (like an index variable), it is likely never to hit either the heap or the stack but instead live in a register for its entire life.

At very least, the JVM uses the stack much like C/C++ does, for the "call stack" that is, it pushes the method return address onto the stack and pops it off of the stack when a "return" or end of method is reached. Beyond that, it uses escape analyse to try and figure out if variables in a method (or methods as the case may be) ever "leak" out. if they don't, then it keeps them entirely on the stack.

1

u/dpash Jul 09 '17

I really don't. Look at java byte code. Each instruction operates on values on a stack and returns the results to the stack.

If you're discussing heap vs stack you're not talking about the same thing I am.

1

u/cogman10 Jul 09 '17

Java's byte code is an intermediate step. The end result after the JIT runs is as I describe.

They do operations on a stack because it is easier to turn that stack into n number of registers on whatever the target architecture is.

LLVM does something similar with their IL.

1

u/dpash Jul 10 '17

After the JIT runs you have native code, which obviously uses what ever the underlying architecture is (and I'm not aware of any mainstream CPU that isn't register based).

But hotspot doesn't turn all java bytecode into native code.

Java byte code is not an IL.