Java and .NET overhead

I have an understanding of how the heap collector and the garbage collector work: garbage collection takes place over generations, memory allocation occurs sequentially, during garbage collection, free / unused space is compacted by shifting data and forming a continuation block, etc.

Are there any headers for the allocated memory chunks and how big are they (I heard it is 8-16 bytes for the .NET CLR), and if there is a byte, word or four-digit alignment? I am interested in any information for implementing JIT (Java) and CLR (.NET Framework or Mono) for x86 and x64 processor architecture.

+3
source share
3 answers

I believe that the size of the header is two words - one for the type reference and one for the synchronization block and other flags. The padding (I think) is enough to round the total size to an integer number of words.

For example, a reference type with only "int" takes 12 bytes on x86, as shown here:

using System;

public class Foo
{
    int x;

    public Foo(int x)
    {
        this.x = x;
    }
}

public class Test
{
    static void Main(string[] args)
    {
        int length = int.Parse(args[0]);

        Foo x = new Foo(0);
        Foo[] array = new Foo[length];
        // Make sure that JITting the string constructor doesn't
        // change things
        long start = GC.GetTotalMemory(true);
        for (int i=0; i < length; i++)
        {
            array[i] = new Foo(i);
        }
        long end = GC.GetTotalMemory(true);

        GC.KeepAlive(array);
        GC.KeepAlive(x);

        decimal totalDecimal = end-start;
        Console.WriteLine(totalDecimal / length);
    }
}

One interesting point - for some reason, an instance of System.Object takes 12 bytes (on x86) instead of 8, which I would otherwise have predicted. As if the minimum size is 12 bytes, but you get the first four bytes of real data :)

, , btw - , , , - . 12, 12, , -, . ( , arg, . .) , , - .

+5

I do not know about Java, but for the CLR there is 1 overhead service information for one reference type. On 32-bit systems it will be 4 bytes and 64-bit systems, it will be 8 bytes.

-2
source

Source: https://habr.com/ru/post/1702822/


All Articles