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

ProgramBytes for 1 000 000 recordsPer itemStable
C#, Release32 000 19232exactly the same on every run
Java, javac + java20 331 976 to 20 546 58420moves 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 next page measures the third thing a runtime does before a program works: start.

Exercises

  1. Change the record to a struct in C# and measure the array again.
  2. Run the Java program with -Xmx256m and see what the maximum heap line says.
  3. Measure an array of a million strings in both and explain the difference.
  4. 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.

  1. What does GC.GetTotalMemory(true) do?

    Answer

    It collects and then reports the managed heap

    The true argument forces the collection before the answer.

  2. How many bytes per item did the C# program print?

    Answer

    32

    The measured line reads per item 32.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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

Banner image