Java ZGC: How Java Moves Objects While Running

Outline

Transcript

0:00 In Java, an object is a bundle of data in memory. When the program can't get back to an object, the garbage collector frees its space. A long pause can slow the whole program. The Z Garbage Collector, or ZGC, mostly works while Java runs. Let's follow one of those objects as ZGC moves it. Then we'll see how a thread, one worker running Java code, can still get stuck when it needs space. In our test, the longest pause shared across the Java workers was nine microseconds, nine millionths of a second.

0:34 What did that number leave out? The main worker waited almost 27 milliseconds, about 27 thousandths of a second. We read the public design and code for OpenJDK, software that runs Java programs, and checked how Netflix put ZGC to work. The description links those sources and our test. First, take the profile in the heap, where Java puts objects. Our two objects are named Request and Session. Each has a field, a named place to store a value. Both hold address A, the location of the profile. A value Java follows to an object is a reference.

1:11 This reference also carries a few on-or-off marks for the collector. That's a colored reference. If the collector moves the profile, does it have to change both of those arrows before Java can run again? Once the profile lands at B, both fields can still hold A. I didn't realize Java could keep going with the old addresses. ZGC records that A now leads to B. That record is a forwarding entry. Okay, so there's a gap between the move and the change to the field. What if a worker tries to get the profile before the collector has recorded the move?

1:47 That worker can try to make the copy. If it can't get room, it waits for a collector worker to finish moving objects from that part of the heap. Then it uses the recorded address to find the profile. Another worker can still build its order. ZGC uses short stops to coordinate, then copies the profile with the program running. Let's see what our worker reads from the field once the move is done. Now the worker reads the Request field. Java uses a check called a load barrier. If the marks say the address is ready, the check hands it straight back.

2:21 What happens if it sees the old address on screen? It follows A to B in the forwarding entry and gives our worker B. It can also fix the stored reference so the next read gets the new address directly. Let's say a second worker points the field at another profile. Could this repair erase that change? Java checks the field and tries to replace the reference in a single action. If the field now points to a different object, Java leaves it alone. Our worker had already read A. It still finds the first profile at B.

2:55 Nobody else wrote in our main example. Request now holds B. Session still holds A. Just one arrow needed to change for that read. Now go back to the waits. In the first row, the longest shared pause for our other collector, G1, was just over 3 milliseconds. With Generational ZGC, which can collect newer objects separately from older ones, the largest pauses ranged from six to nine microseconds. Same repeated memory test and limit each time. My first thought was to close the results and call it fixed.

3:29 What is that worker waiting for in the second row? Still stuck, even with the window closed. The worker needed space for more data. Java's written event record, the log, calls that an allocation stall. Every ZGC run had one and the longest was about 29 milliseconds. Would you pick G1 because its pause was shorter than that wait? We'd need to compare the same kind of wait. G1 didn't record a matching entry. These rows cover the whole run, including setup. When work slows down, I'd look for the delay that overlaps it.

4:03 So imagine an online checkout during a rush. One order hits its time limit and the shopper sees an error. The screen shows a short pause and a wait for memory. What tells you which one affected that order? I'd line up the events by time. On this timeline, the order went to worker W. W ran short of room while handling it. The shared pause came later, so I'd check how full the heap was when W needed more. At Netflix, one group of computers using Generational ZGC stopped fewer tasks for taking too long, so fewer had to run again.

4:39 Some jobs with bursts of memory use did better with G1. Would you switch for this checkout? I'd give both collectors the same orders and memory, with the same number of processors to run the code. I'd measure how long orders take, how much time the processors spend working, and how close we get to filling the heap. Thanks for listening to Learning Podcasts.