Tech News
← Back to articles

Help, My Java Object Vanished (and the GC Is Not at Fault)

read original related products more articles

Today I’m going to talk about a recent journey as a HotSpot Java Virtual Machine developer working on the OpenJDK project. While running tests for a new feature, I realized my Java objects and classes were arbitrarily disappearing! What followed was probably the most interesting debugging and fixing experience of my life (so far), which I wanted to share with the world.

This post is targeted towards a wider (computer science) audience. Knowledge about Java or the JVM is not expected, but a slight curiosity towards low-level programming is encouraged. My intentions with this essay are to:

introduce Project Valhalla and value objects; give insights into the inner workings of HotSpot (& hopefully motivate folks to contribute); pragmatically demonstrate how JVM flags can be used to help you, the developer; teach some lessons I learned in debugging; document processes for my future self/colleagues; and give myself an opportunity to yap about an achievement (and draw bad ASCII art).

I’ve written a lot. I’ve included summaries for each of the sections, sans takeaways. They’re designed to be coherent, so use them to your advantage and read what you find interesting, although I truly believe there is value in reading the entire post.

So what was I doing? I was changing the markWord to conform to the format specified by JEP450 for Project Valhalla. It’s likely these terms won’t mean anything to you, so let me break it down.

Java 101#

Java is a multi-purpose programming language. It is compiled to platform-independent Java bytecode. This bytecode is then executed by a Java Virtual Machine, which performs, among other things, automatic garbage collection. The reference implementation of the JVM is called HotSpot. It features just-in-time compilation, which compiles (some) frequently executed Java methods into native code for performance gains.

Object Headers#

Java objects reside in the heap. Each object has metadata associated with it, which is stored in the object header. If there’s anything I want you to take away from this, it’s that there are some bits in there that are very interesting.

Traditionally, the object header consists of the mark word (called markWord in source code) and class word (contains the class pointer, which is totally irrelevant to this blog post), totaling 96-128 bits on 64-bit architectures. I’ll go more in-depth about the mark word soon.

... continue reading