1
0

Part 6: 1M-record generator with measured heap delta

This commit is contained in:
2026-08-04 17:33:29 +00:00
parent 978d32bbb4
commit 6e6f6c48ff

View File

@@ -0,0 +1,55 @@
package com.ankurm.jackson3.part6streaming;
import tools.jackson.core.JsonEncoding;
import tools.jackson.core.JsonGenerator;
import tools.jackson.core.ObjectWriteContext;
import tools.jackson.core.json.JsonFactory;
import java.io.File;
/**
* Post: Streaming API and JsonNode — https://ankurm.com/jackson-streaming-api-jsonnode/
* Section: "Writing JSON with JsonGenerator"
*
* Jackson 3 differences: writeNumberField/writeStringField are now
* writeNumberProperty/writeStringProperty, and the factory needs an
* ObjectWriteContext. The post's 1,000,000-record loop is kept — it is the whole
* point of streaming — and the peak heap is measured so "constant memory" is a
* number rather than a claim.
*/
public class G02StreamingGenerator {
private static final int RECORD_COUNT = 1_000_000;
public static void main(String[] args) throws Exception {
File output = File.createTempFile("output", ".json");
output.deleteOnExit();
JsonFactory jsonFactory = new JsonFactory();
Runtime runtime = Runtime.getRuntime();
long before = runtime.totalMemory() - runtime.freeMemory();
long start = System.nanoTime();
try (JsonGenerator generator = jsonFactory.createGenerator(
ObjectWriteContext.empty(), output, JsonEncoding.UTF8)) {
generator.writeStartArray();
for (int recordIndex = 0; recordIndex < RECORD_COUNT; recordIndex++) {
generator.writeStartObject();
generator.writeNumberProperty("id", recordIndex);
generator.writeStringProperty("status", "active");
generator.writeEndObject();
}
generator.writeEndArray();
}
long elapsedMs = (System.nanoTime() - start) / 1_000_000;
long after = runtime.totalMemory() - runtime.freeMemory();
System.out.println("records written : " + RECORD_COUNT);
System.out.println("file size : " + (output.length() / 1024 / 1024) + " MB");
System.out.println("elapsed : " + elapsedMs + " ms");
System.out.println("heap delta : " + ((after - before) / 1024 / 1024) + " MB"
+ " <- the document is never held in memory");
}
}