Hasty Briefsbeta

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

13 days ago
  • #JVM
  • #Java
  • #Debugging
  • The author, a HotSpot Java Virtual Machine developer, encountered an issue where Java objects and classes were disappearing during tests for a new feature related to Project Valhalla.
  • Project Valhalla introduces value objects, which are objects distinguished by their fields, enabling optimizations like heap flattening and scalarization.
  • The issue stemmed from changes to the markWord format to comply with JEP 450 (Compact Object Headers), which reorganized metadata bits in the object header.
  • The bug manifested as widespread, intermittent test failures, including null objects when they shouldn't be and NoClassDefFoundErrors, primarily when using the C2 JIT compiler without Compact Object Headers enabled.
  • Debugging involved narrowing down the issue to a miscompilation in the Object::hashCode intrinsic, caused by an incorrect bitmask checking native pointer bits instead of metadata bits.
  • The root cause was an alignment issue where the faulty bitmask only manifested after moving the value object bit due to native memory alignment.
  • The fix involved correcting the bitmask to only check the lock bits, resolving the symptoms of null objects and missing class definitions.
  • Key takeaways include the importance of methodology in debugging, asking the right questions, and leveraging tools effectively.