Java和.NET堆开销 [英] Java and .NET heap overhead

查看:222
本文介绍了Java和.NET堆开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解如何堆和垃圾收集器的工作原理:垃圾收集发生在代内存分配过程中通过移动数据和形成压缩垃圾收集自由/未使用的空间发生顺序,继续块等。

I have understanding how heap and garbage collector works: garbage collection happens in generations, memory allocation happens sequentially, during garbage collection free/unused space compacted by shifting data and forming continues block, etc.

有没有什么头文件分配的内存块是present,他们有多大(听说是8-16字节.NET CLR),如果字节,字或四字对齐present?我感兴趣的任何信息JIT(JAVA)和CLR(.NET Framework或单声道)实现x86和x64处理器架构。

Is there any headers for allocated memory chunks are present and how big are they (I heard it’s 8-16 bytes for .NET CLR) and if byte, word or quad-word alignment present? I’m interested in any information for JIT (Java) and CLR (.NET Framework or Mono) implementation for x86 and x64 processor architectures.

推荐答案

我相信头的大小为两个词 - 一个用于类型引用,一个用于同步块和其他标志。该填充是(我相信)刚好能圆总规模高达字的整数。

I believe the header size is two words - one for the type reference and one for the sync block and other flags. The padding is (I believe) just enough to round the total size up to a whole number of words.

例如,在短短一个INT引用类型需要在x86 12字节,这里演示:

For instance, a reference type with just an "int" in takes 12 bytes on x86, as demonstrated 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);
    }
}

一个有趣的一点 - 由于某种原因System.Object的实例需要12个字节(在x86上),而不是8,我本来有predicted。这是因为如果最小尺寸为12个字节,但你的前四个字节的真实数据是免费的:)

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

我不知道为什么报道的尺寸不完全是一个整数,顺便说一句 - 我怀疑它的东西做的,在托管堆中要求每页额外的内存,或者类似的东西一点点。有时,结果是一点点超过12,12岁以下,有时一点点 - 这似乎取决于特定的长度。 (这个答案的previous版本有一个错误,它会分析第一个命令行参数,但再忽视它。我已经固定的)。无论如何,我不认为这有点不准有什么关系与在存储器的个体对象的大小。

I don't know why the size reported isn't exactly an integer, btw - I suspect it's something to do with a little bit of extra memory required for per page in the managed heap, or something like that. Sometimes the result is a little bit over 12, sometimes a little bit under 12 - that seems to depend on the length given. (The previous version of this answer had a bug in, where it would parse the first command line arg but then ignore it. I've fixed that.) Anyway, I don't believe this slight inaccuracy has anything to do with the size of an individual object in memory.

这篇关于Java和.NET堆开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆