
JVM Internals (2013) - flying_sheep
http://blog.jamesdbloom.com/JVMInternals.html
======
mr_overalls
Can anyone compare the JVM's technical merits with Microsoft's CLR? I always
hear that the JVM compared to advanced alien technology, with its millions of
engineer-hours, but not so much glowing praise about the CLR.

With Oracle remaining evil and Microsoft making waves in open source these
days, I wonder how likely it is, for example, that a Big Data ecosystem like
Hadoop/Spark/etc. could evolve around C# and .NET.

~~~
gecko
They’re honestly not quite miscible. I know that, when .NET came out, everyone
responded with “haha they copied Java,” but the truth is that they make a pile
of very different technical trade-offs. The CLR has explicit stack allocation,
the JVM does not. The JVM is genuinely a JIT, the CLR is really all about AOT.
The CLR has full support for low-level code (pointers and such), while the JVM
does not. The JVM and its class library has been cross-platform from day one,
the CLR in a meaningful sense has not. The CLR has a really simple FFI, the
JVM traditionally has not. The CLR has reified generics, the JVM does not. The
JVM has a much more advanced GC, the CLR’s use of value types and things like
Span<> means that such an advanced GC is (at least arguably) not necessary,
and so on.

I really love the CLR, and I think it’s just as much “alien technology” as the
JVM. I suspect the lack of love for it likely has more to do with the official
versions being Windows-only than anything else. But I think it’s also just
true that, while they both fill the same niche in a grand way (they’re both
high-level VMs), their details are sufficiently different that a direct
comparison doesn’t honestly make a ton of sense.

~~~
eikenberry
IMO pretty much no other language run-time is directly comparable to the JVM
simply due to the VM part. Building an entire VM into your run-time comes at a
huge complexity cost but is necessary for its goal of running on different
hardware seamlessly. It is simply not worth it for most run-times to try for
that level of binary compatibility when simpler solutions like cross compiling
work most of the time.

~~~
jcrites
Many other language runtimes (besides the Java JVM) are implemented as virtual
machines:

Python's most widely used CPython implementation uses a VM:
[https://leanpub.com/insidethepythonvirtualmachine/read](https://leanpub.com/insidethepythonvirtualmachine/read)

Ruby "compiles the abstract syntax tree into lower-level byte code. This byte
code is then run by the Ruby virtual machine.":
[http://blog.honeybadger.io/how-ruby-interprets-and-runs-
your...](http://blog.honeybadger.io/how-ruby-interprets-and-runs-your-
programs/)

Perl 6 runs on the Parrot VM:
[https://perl6.org/archive/architecture.html](https://perl6.org/archive/architecture.html)
(and I believe Perl 5 had a different stack-based VM)

These days, more interpreted languages use VMs than not. One notable language
runtime that does _not_ use a virtual machine is the Chrome V8 JavaScript
engine, which I believe compiles JavaScript directly into machine code and
then executes it.

~~~
jashmatthews
V8 switched to an interpreter last year. It's a really interesting project:
[https://v8project.blogspot.com/2017/05/launching-ignition-
an...](https://v8project.blogspot.com/2017/05/launching-ignition-and-
turbofan.html)

------
akerro
I also recommend this JVM anatomy [https://shipilev.net/jvm-anatomy-
park/](https://shipilev.net/jvm-anatomy-park/)

~~~
le-mark
This is also good, but dated:

[https://www.artima.com/insidejvm/ed2/](https://www.artima.com/insidejvm/ed2/)

~~~
akerro
Shame it's not complete.

------
kjeetgill
Wow! Amazing find. This is an amazingly well organized, detailed, useful
resources.

It might be worth adding (2013) or JDK7 to the title. Off the top of my head
the details about the permgen don't apply anymore and I believe this predates
G1.

~~~
pjmlp
And in any case very specific to one JVM implementation, while there is a
plethora of them around.

[https://en.wikipedia.org/wiki/List_of_Java_virtual_machines#...](https://en.wikipedia.org/wiki/List_of_Java_virtual_machines#Active)

[https://en.wikipedia.org/wiki/List_of_Java_virtual_machines#...](https://en.wikipedia.org/wiki/List_of_Java_virtual_machines#Active_2)

~~~
bitcharmer
It's true, but in practice Oracle's Hotspot is used in overwhelming majority
of deployments you will get to work on.

~~~
pjmlp
Not when you work with corporations that love Websphere, embedded deployments
or that little fork known as Android.

~~~
jxub
I recently attended a talk with Simon Ritter from Azul Systems that is
developing Zing VM, and this VM is apparently gaining adoption among financial
companies thanks to improved startup time and novel GC algorithms. Anyone with
first hand production experience?

~~~
yazr
Yes. Used in production HFT system. Works as advertised.

But you should try the new J8 GC algos before you try a whole new VM

------
rootlocus
I also enjoyed the book "The Java Virtual Machine Specification" [1] which is
available on Safari Online.

[1] [https://www.amazon.com/Java-Virtual-Machine-Specification-
Ad...](https://www.amazon.com/Java-Virtual-Machine-Specification-Addison-
Wesley-ebook/dp/B00K3NR6M4)

~~~
gunnarmorling
The spec for the JVM (as the one for the Java language) is also freely
available here:
[https://docs.oracle.com/javase/specs/index.html](https://docs.oracle.com/javase/specs/index.html)

------
SegFaultCDumped
Can we make hardware that runs JVM bytecode? It's a neat idea.

~~~
joshumax
Certain ARM-based SoCs used to be able to do this using a feature called
Jazelle[1], but it had an array of problems that caused an early obsolescence.

1:
[https://en.wikipedia.org/wiki/Jazelle](https://en.wikipedia.org/wiki/Jazelle)

------
pvg
Previouslies:
[https://hn.algolia.com/?query=jvm%20internals&sort=byPopular...](https://hn.algolia.com/?query=jvm%20internals&sort=byPopularity&prefix&page=0&dateRange=all&type=story)

There's nothing really wrong with this popular write-up and its many ripoffs
and copycats over the years, other than the (in hindsight) poorly chosen
title. Most of this stuff is an overview of what's in the JVM spec. If you are
searching around for some thing or another about the actual internal
implementation details of the Oracle JVM, this article and its many clones
make the search term 'JVM internals' nigh-useless.

------
javajosh
As a serious Java nerd now programming python, I'd love to see something like
this for python!

~~~
simen
You could begin with 500 lines or less:
[http://www.aosabook.org/en/500L/a-python-interpreter-
written...](http://www.aosabook.org/en/500L/a-python-interpreter-written-in-
python.html)

It implements a naive, simplified Python VM in Python. Of course, that is only
a very high-level overview. By necessity it handwaves a ton by just relying on
the underlying Python (like GC). For a more in-depth look, you can check out
PyPy's dev docs
([http://doc.pypy.org/en/latest/](http://doc.pypy.org/en/latest/)). PyPy is an
alternative to CPython, so it does many things very differently (some in a way
that have never been done in prog lang implementation before), but the docs do
make comparisons to CPython.

------
jokoon
This reminded of the "layers of crap" John Carmack was talking about once.

Java is an impressive tech, but I still fail to understand why it is so
popular. It comes from a proprietary tech, it requires additional software to
run, you cannot disable its garbage collector, browsers don't like it, I don't
think it's very popular in the manufacture embedded controllers, and if
enforces object oriented structure inside its JVM. Oh and Torvalds hates it
too.

Not to mention the gargantuan bloat that are JEE and system like maven or ant.
It feels really different and foreign compared to the simplicity of unix many
devs are used to.

Even microsoft decided to rewrite minecraft to C++. I saw a private server had
such big freezes, just to handle 3 or 4 players.

~~~
jmartrican
> it requires additional software to run

Many languages require additional software to run.

> Java is an impressive tech, but I still fail to understand why it is so
> popular.

I used to write C++ as my goto general purpose language. After using Java once
I was hooked. It was much easier to do stuff and manage my dependencies.

I think Java has 3 things going for it.

1) The ecosystem is huge. To dethrone Java for backend do not just compare
another language to Java, you have to compare its ecosystem. Java has a huge
ecosystem especially with Spring Boot tieing many of the most popular parts of
that ecosystem together.

2) Speed. Some languages are easy to use. Some are very fast. It seems to me
that if one were to quantify ease and speed and plot a min-max graph, Java
would be up at the top... best performance with ease of use.

3) Here I am going to throw a hodgepodge of things that can be argued but when
taken together amounts to a big deal. Reliability (its used in so many places
that by the time you go to write your code rest-assured you are not stumbling
onto a new bug), write once, swap JVM's, advanced garbage collection (G1 and
beyond, GC is something that is constantly being tweaked and improved),
evolution (Java evolves with the times, it biggest gripes became the next
thing being addressed), Maven/Gradle (advanced dependency management and build
orchestration... these tools are very powerful), Junit/mockito and other
testing tools (very powerful testing suites).

~~~
victor106
+1 for Spring boot and the Spring ecosystem in general. I said it here a few
weeks back, Pivotal(the company behind Spring) continue to keep that ecosystem
innovative, modern, stable and performant and open sourced. That’s amazing...

We had a few rails developers work port some of their apps to Spring Boot,
they struggled initially but after getting over that hump their feedback was
it’s light years ahead of rails in terms of ease of use and performance. Easy
of use = Groovy & Kotlin both of which compile to the JVM

The real game changer is when Spring runs on GraalVM. Once that happens it
would be really really hard to beat java on the backend

~~~
tannhaeuser
I agree Spring has for most intents and purposes won the framework wars (which
however also speaks to the relative stagnation in that space). What do you
specifically expect from Spring running on GraalVM?

I'm not sure about the availability and maturity of the new-fangled
Graal/Truffle/SubstrateVM stuff. I've being eying migration strategies _away_
from the JVM for quite some time now, and towards JavaScript specifically due
to its momentum and portability. Both Oracle and RedHat (or their employees as
personal projects) had been running efforts for a node.js-compatible runtime
system on the JVM several years ago. Now GraalVM provides a polyglot runtime
for JavaScript and Ruby, but my understanding is it'll be commercial-only once
it's released. So in a way, Oracle is preparing a Java exit strategy
themselves :)

~~~
victor106
> What do you specifically expect from Spring running on GraalVM?

Mostly performance gains you get from AOT. I think GraalVM ( or similar
technologies) is going to be a game changer for all langs that compile to
intermediary byte code.

Unfortunately, GraalVM does not compile Spring as Spring uses reflection a lot
and GraalVM does not fully support reflection.
[https://github.com/oracle/graal/issues/348](https://github.com/oracle/graal/issues/348)

~~~
fcurts
Performance gains of GraalVM's AOT are drastically reduced startup time (1 ms
for my Java/Kotlin command-line applications), elimination of JIT warmup time,
and reduced memory footprint. For peak performance of a language as dynamic as
Java, a JIT is hard to beat. Profile-guided optimizations will narrow the gap
but are only available in GraalVM Enterprise Edition. Native executables also
have a less sophisticated GC than HotSpot.

~~~
victor106
Thank you for sharing your knowledge. Its insights like this that make hn so
awesome.

------
jacksmith21006
This is the type of post I love the most.

Great find and wish we got more things like this.

