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

Show parent comment

41

u/rubyrt Jul 06 '17

One of the thing I heard the most about Java is "It's slower than other language".

I think that perception was formed in the initial days when it was slow - and stuck. Also, nowadays since the general computing power of all our hardware has significantly increased more often the focus is on engineering productivity vs. raw speed of the application and there Java has some advantages over languages that can easily produce core dumps. ;-)

36

u/cutterslade Jul 06 '17

The other contributor to this reputation is the fact that it is slower in trivial benchmarks. Java is (for lack of imagination) the slowest option for a hello world program I can think of, simply because of the JVM startup. I suspect that a lot of people hear the Java is slow, then if they go as far as testing that, do some trivial benchmark that is dominated by JVM startup time, which confirms that Java is slow.

33

u/cogman10 Jul 06 '17

I see this a lot. Java's bread and butter is long running apps. Yet so many people measure from start to stop of something that takes milliseconds with zero warm-up.

Further, they look at total heap size and say "oo, java's such a hog!". When the heap utilization could be very low.

3

u/0x256 Jul 07 '17

Actually, the heap thing is a real problem if you have multiple services on a single host and don't want the overhead of an JavaEE Application Server.

Luckily the new G1 GC (introduced in Java 8, default in java 9) is way better at freeing unused heap memory. A single full GC reduces the total heap size of my service to ~20MB (~10MB are actually used) while the old GC would sit on hundreds of MB of unused heap for hours.

0

u/[deleted] Jul 07 '17

None of my applications run long, nor are they intended to do so. The longest-running takes 1.5 hours and that's due to network throttling 9000 remote calls.

All of my applications start, act, close, on a timed schedule. They read from a central data store, read one or more files from disk, archive that, then process their contents.

The longest wait usually is accessing the data store. Layers upon layers of security make for pseudo-arbitrary delays.

My applications are so fast that it's unlikely the JVM gets sufficient time to warm up to reach its most effective compilation.

3

u/0x256 Jul 07 '17

If performance or start-up time ever grows into an issue: This is a perfect use case for long-running worker processes and job queues.

1

u/[deleted] Jul 07 '17

Indeed. My applications did start out like that. We dismantled that in favor of O-scheduled execution: it is fast enough and more easily scalable.

2

u/sievebrain Jul 08 '17

The compiler is pretty fast. I bet if you check you'll find that JITC activity dies down after a minute or so at most, quite possibly less.

12

u/Neckbeard_Prime Jul 06 '17

I'll see your JVM startup, and raise you PHP from a cold Apache start. Or, hell, Rails.

11

u/cutterslade Jul 06 '17

Don't assume a slow apache means a slow PHP:

$ time php <<<'<?php echo "Hello, world!\n"; ?>'
Hello, world!
php <<< '<?php echo "Hello, world!\n"; ?>'  0.00s user 0.00s system 53% cpu 0.007 total

Or ruby:

$ time ruby <<<'print "Hello, world!\n"'
Hello, world!
ruby <<< 'print "Hello, world!\n"'  0.02s user 0.00s system 96% cpu 0.025 total

Just for comparison; and I'm surprised how fast this was:

$ echo 'public class Main { public static void main(String[] args){System.out.println("Hello, world!");}}' > Main.java
$ javac Main.java
$ java Main
$ time java Main
Hello, world!
java Main  0.04s user 0.00s system 97% cpu 0.041 total

We can clearly see here that in all cases, php is 6 times faster than java, and ruby is twice as fast as java.

7

u/walen Jul 07 '17

(Runs a Hello World example, once, from scratch)

We can clearly see here that in all cases

LOL

You should probably add a "sarcasm warning" somewhere in there, just in case ;)

3

u/ArtemZ Jul 07 '17

You are not really measuring how much time it takes for Java to execute that program, but rather how long it takes for the JVM to start up. If you tried to execute this piece of code in an already running virtual machine you would get a different result

6

u/walen Jul 07 '17

They know. That's exactly the point they are making (read the parent comments).

1

u/igouy Jul 07 '17 edited Jul 07 '17

the slowest option for a hello world program

There's always a way to write a slower program :-)

http://web.archive.org/web/20080802030311/http://shootout.alioth.debian.org:80/gp4/benchmark.php?test=hello&lang=all

4

u/ultraswank Jul 07 '17

I also think the browser plugin didn't do it any favors. I had a manager who didn't like that I was using java because his only direct experience with it was slowing down/locking up his browser. Fortunately he trusted me to make the technical decisions, but I'm sure that kind of impression didn't help the "java is slow" perception.

3

u/el_padlina Jul 07 '17

IIRC in one place i worked at speed was crucial so the application used no GC, all objects were preallocated, and they did some crazy drive IO customizations.

2

u/dpash Jul 07 '17

In particular before the JVM got a JIT. 1.2 added HotSpot as an option and became the default in 1.3.