Memory, measured: C# and Java
Allocation is the other half of a performance question: a program that allocates a million objects pays for them in the heap and in the collections that follow. The task is one array of a million records with one integer in each, measured before and after the allocation, with a forced collection on both sides so the number is the live set and not the litter.
The task in C#
GC.GetTotalMemory(true) collects first and then answers, so the two numbers are comparable:
Console.WriteLine("memory 1.0");
var before = GC.GetTotalMemory(true);
var items = new Item[1_000_000];
for (var i = 0; i < items.Length; i++)
{
items[i] = new Item(i);
}
var after = GC.GetTotalMemory(true);
Console.WriteLine($"items {items.Length}");
Console.WriteLine($"bytes {after - before}");
Console.WriteLine($"per item {(after - before) / items.Length}");
Console.WriteLine($"generation {GC.GetGeneration(items)}");
Console.WriteLine($"first {items[0]}");
record Item(int Id);
dotnet run
echo "status: $?"
memory 1.0
items 1000000
bytes 32000192
per item 32
generation 2
first Item { Id = 0 }
status: 0
The same task in Java
The JVM has no GetTotalMemory; the closest is totalMemory() - freeMemory() after a System.gc(), which is a request and not a command:
public class Memory {
record Item(int id) {
}
public static void main(String[] args) {
System.out.println("memory 1.0");
Runtime runtime = Runtime.getRuntime();
System.gc();
long before = runtime.totalMemory() - runtime.freeMemory();
Item[] items = new Item[1_000_000];
for (int i = 0; i < items.length; i++) {
items[i] = new Item(i);
}
System.gc();
long after = runtime.totalMemory() - runtime.freeMemory();
System.out.println("items " + items.length);
System.out.println("bytes " + (after - before));
System.out.println("per item " + ((after - before) / items.length));
System.out.println("max heap " + runtime.maxMemory() / (1024 * 1024) + " MB");
System.out.println("first " + items[0]);
}
}
javac -g -d out Memory.java
java -cp out Memory
echo "status: $?"
memory 1.0
items 1000000
bytes 20339000
per item 20
max heap 2920 MB
first Item[id=0]
status: 0
The numbers
| Program | Bytes for 1 000 000 records | Per item | Stable |
|---|---|---|---|
| C#, Release | 32 000 192 | 32 | exactly the same on every run |
| Java, javac + java | 20 331 976 to 20 546 584 | 20 | moves by about a percent |
The C# number is twice the object header plus the integer plus the reference in the array: 24 bytes for the record and 8 for the slot, which is the 32 the program prints. It does not move because GC.GetTotalMemory(true) forces the collection and reports the managed heap exactly.
The Java number is smaller and approximate. A record with one int in a 64-bit JVM is 24 bytes with the header and the compressed reference in the array, but the heap figure includes the allocator's own bookkeeping and the objects the JVM created while the program ran, and System.gc() is a suggestion. The maximum heap of this JVM is 2 920 MB, which the program prints: a quarter of the memory of the machine, chosen by the JVM from the container's limits.
What differs
- The C# collector is generational and compacting, and the measurement of the live set is part of its API. The JVM has several collectors and a heap figure that a program can only approximate; the right tool there is a profiler, not a subtraction.
- A record with one integer costs 24 bytes in both, which is the price of an object header on a 64-bit machine. A C# struct of one integer costs four bytes inside an array and nothing else, which is why the page of value types exists; Java has no such array of values except the primitives.
- The heap limit is a run-time choice in both. C# has DOTNET_GCHeapHardLimit and the GC modes of lesson 068; Java has -Xmx and the choice of a collector, and the JVM reads the container limit by default, which is where 2 920 MB came from.
The next page measures the third thing a runtime does before a program works: start.
Exercises
- Change the record to a struct in C# and measure the array again.
- Run the Java program with -Xmx256m and see what the maximum heap line says.
- Measure an array of a million strings in both and explain the difference.
- Find the collector of this JVM with -XX:+PrintFlagsFinal and name it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does GC.GetTotalMemory(true) do?
Answer
It collects and then reports the managed heap
The true argument forces the collection before the answer.
-
How many bytes per item did the C# program print?
Answer
32
The measured line reads per item 32.
-
What does this program print?
Console.WriteLine(sizeof(int));Answer
4
Four bytes of an int in an array, which the page compares with 24.
-
Why does the Java number move between runs?
Answer
The JVM heap figure includes its own bookkeeping and System.gc is a request
The page quotes a range of about a percent.
-
What does this program print?
var items = new Item[1_000_000]; Console.WriteLine(items.Length); record Item(int Id);Answer
1000000
An array of a million records has a million elements.
-
What is the object header cost on this runtime?
Answer
About 24 bytes for a record with one int
The page derives the 24 from the measured 32 with the array slot.
-
What does this program print?
var points = new[] { new Small(1), new Small(2), new Small(3), new Small(4) }; Console.WriteLine(points.Length); struct Small(int x) { public int X { get; } = x; }Answer
4
A struct of one int in an array takes four bytes.
-
How is the Java maximum heap chosen?
Answer
By the JVM from the container limit, unless -Xmx says otherwise
The measured line reads max heap 2920 MB.
-
What does this program print?
var items = new Item[3]; Console.WriteLine(items.Length - 3); record Item(int Id);Answer
0
The array is empty before the loop fills it.
-
Which tool gives an exact live-set figure on the JVM?
Answer
A profiler, not a subtraction of two heap numbers
The page says the subtraction is an approximation.
The C# program was built with the .NET SDK 10.0.303 and the Java program with OpenJDK 21.0.13.1 (javac 21.0.13.1), both run on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev