How ZGC allocates memory for the Java heap

https://news.ycombinator.com/rss Hits: 7
Summary

2025-04-16 How ZGC allocates memory for the Java heap This post explores how ZGC, one of the garbage collectors in the OpenJDK, allocates memory for the Java heap, focusing on enhancements introduced in JDK-8350441 with the Mapped Cache. A garbage collector does much more than just collect garbage - and that’s what I want to unpack in this post. Whether you’re a Java nerd yearning for details, a GC enthusiast, or just curious about how ZGC uses memory behind the scenes, this deep dive is for you. The features described in this post are lined up for the release of OpenJDK 25. You can download the latest version of the OpenJDK here. To see the latest version of ZGC, you should look at the mainline source code. Some particularly relevant source files include: The content in this post is generally applicable to all operating systems and platforms that ZGC runs on. Additional Linux-specific details are mentioned where relevant. For example, ZGC only supports NUMA on Linux, not on Windows and BSD. Background In ZGC, memory for the Java heap is organized into logical regions known as pages. These should not be confused with operating system (OS) pages. From this point on, any reference to pages refers specifically to ZGC pages unless explicitly stated otherwise. Pages are categorized into three size classes: Small, Medium, and Large. This classification helps optimize memory usage and allocation strategies based on object size. Small pages are 2 MB and are used for most regular object allocations. In a typical Java program, most allocations are small and will end up on a Small page. Medium pages are generally 32 MB and are intended for larger objects that don’t quite qualify as “large”. Large pages are dynamically sized and are used for very large allocations, typically over 4 MB. Only a single object is stored on a Large page. Memory for the Java heap, and in turn pages, is managed by the Page Allocator. The main part of allocating a page is getting hold of its underlying...

First seen: 2025-04-23 16:46

Last seen: 2025-04-23 22:48