Urgent.News

What's breaking now, across thousands of outlets.

Tech

What Go Taught Us About Java Garbage Collection

As Java developers, we take a lot of inspiration from HotSpot. It is the gold standard for many of us, and for good reason. But measuring ParparVM against Go recently sent me down a different path. What is Codename One? Codename One is an open-source framework for building native iOS, Android, desktop, and web apps from a single Java or Kotlin codebase. Learn more at codenameone.com . My first…

Java developers often look to HotSpot as the gold standard for their work, but comparing Go's runtime to Java's has led to new insights. Go keeps more objects off the heap, which significantly reduces the work for the garbage collector. This attraction resonates with Java developers who appreciate the value objects concept in Go. While allocation isn't the full explanation, Go's runtime decisions about memory collection and sharing can offer valuable lessons for Java as well.

ParparVM, a Java-to-C compiler, provides a unique opportunity to experiment with these ideas. By controlling object layout, reference handling, and the collector itself, ParparVM can explore the benefits of Go's approach. The investigation began with adjusting the memory allowance for garbage to see how it impacted the overall system.

The minimum allocation threshold was set at 24 MB. Lowering this threshold led to a reduction in memory usage from 98 MB to 38 MB, which significantly improved performance. This suggests that a more adaptive memory allowance strategy could be beneficial for Java applications too.

However, setting this threshold dynamically based on the live set of objects proved challenging. The current garbage collection system in ParparVM doesn't accurately count all live objects, leading to suboptimal memory allocation decisions. To address this, the floor for the minimum allocation threshold has been made configurable in the runtime, allowing developers to experiment with different settings based on their specific workload.

Another area of focus was the image cache, which can consume significant memory and CPU resources. The experiment demonstrated that reducing the memory allowance for the image cache improved performance without sacrificing speed. To further optimize this, the collector now supports weak and soft references, with a policy that favors recently accessed data. This change helps the collector prioritize memory management based on actual usage patterns rather than treating all cached data as permanent storage.

The experiment also explored mutator assistance, where allocating threads can perform marking work when they reach a certain limit. This allows the garbage collector to continue its work without waiting for the thread to finish. Additionally, the parallel marking feature has been enhanced to further reduce pause times, bringing them closer to the efficiency seen in Go's runtime.

Overall, this investigation highlights the potential benefits of adopting some of Go's garbage collection strategies in Java. By fine-tuning memory allocation thresholds, leveraging weak and soft references, and implementing mutator assistance, Java developers can achieve better performance and more efficient memory management.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

VibeX: the architecture diagram you never have to draw

ERDs, C4 models, API catalogues and state machines generated from the schema that actually ships — plus documentation that fails CI when it drifts.

  • VibeX generates architecture diagrams from schema files
  • HTML file contains diagrams, documentation, and source code links
  • Diagrams stay in sync with actual schema

More from Saturday 19 September →