c#中的对象内存数量消耗多少?

时间:2022-01-20 01:16:00

Suppose that we have previously instantiated three objects A, B, C from class D now an array defines as below: D[] arr = new D[3]; arr[0]=A; arr[1]=B; arr[2]=C;

假设我们先前已经从D类实例化了三个对象A,B,C,现在数组定义如下:D [] arr = new D [3]; ARR [0] = A; ARR [1] = B; ARR [2] = C;

does array contains references to objects or has separate copy?

数组是否包含对象的引用或具有单独的副本?

5 个解决方案

#1


3  

C# distinguishes reference types and value types.

C#区分引用类型和值类型。

A reference type is declared using the word class. Variables of these types contain references, so an array will be an array of references to the objects. Each reference is 4 bytes (on a 32-bit system) or 8 bytes (on a 64-bit system) large.

使用单词class声明引用类型。这些类型的变量包含引用,因此数组将是对象的引用数组。每个引用都是4个字节(在32位系统上)或8个字节(在64位系统上)大。

A value type is declared using the word struct. Values of this type are copied every time you assign them. An array of a value type contains copies of the values, so the size of the array is the size of the struct times the number of elements.

使用单词struct声明值类型。每次分配时都会复制此类型的值。值类型的数组包含值的副本,因此数组的大小是struct的大小乘以元素的数量。

Normally when we say “object”, we refer to instances of a reference type, so the answer to your question is “yes”, but remember the difference and make sure that you don’t accidentally create a large array of a large struct.

通常,当我们说“对象”时,我们引用引用类型的实例,因此您的问题的答案是“是”,但请记住差异并确保您不会意外地创建大型结构的大型数组。

#2


5  

An array of reference types only contains references.

引用类型数组仅包含引用。

In a 32 bit application references are 32 bits (4 bytes), and in a 64 bit application references are 64 bits (8 bytes). So, you can calculate the approximate size by multiplying the array length with the reference size. (There are also a few extra bytes for internal variables for the array class, and some extra bytes are used for memory management.)

在32位应用程序中,引用是32位(4字节),而在64位应用程序中,引用是64位(8字节)。因此,您可以通过将数组长度乘以参考大小来计算近似大小。 (对于数组类,内部变量还有一些额外的字节,一些额外的字节用于内存管理。)

#3


2  

You can look at the memory occupied by an array using WinDBG + SOS (or PSSCOR2). IIRC, an array of reference types is represented in memory by its length, followed by references to its elements, i.e. it's exact size is PLATFORM_POINTER_SIZE * (array.Length + 1)

您可以使用WinDBG + SOS(或PSSCOR2)查看数组占用的内存。 IIRC,引用类型数组在内存中以其长度表示,然后引用其元素,即它的确切大小为PLATFORM_POINTER_SIZE *(array.Length + 1)

#4


0  

The array is made out of pointers (32bit or 64bit) that points to the objects. An object is a reference type, only value types are copied to the array itself.

该数组由指向对象的指针(32位或64位)组成。对象是引用类型,只有值类型被复制到数组本身。

#5


0  

As @Yves said it has references to the objects. The array is a block of memory as it as in C. So it size is sizeof(element) * count + the amount of memory needed by oop.

正如@Yves所说它有对象的引用。该数组是一个内存块,就像在C中一样。所以它的大小是sizeof(元素)* count + oop所需的内存量。

#1


3  

C# distinguishes reference types and value types.

C#区分引用类型和值类型。

A reference type is declared using the word class. Variables of these types contain references, so an array will be an array of references to the objects. Each reference is 4 bytes (on a 32-bit system) or 8 bytes (on a 64-bit system) large.

使用单词class声明引用类型。这些类型的变量包含引用,因此数组将是对象的引用数组。每个引用都是4个字节(在32位系统上)或8个字节(在64位系统上)大。

A value type is declared using the word struct. Values of this type are copied every time you assign them. An array of a value type contains copies of the values, so the size of the array is the size of the struct times the number of elements.

使用单词struct声明值类型。每次分配时都会复制此类型的值。值类型的数组包含值的副本,因此数组的大小是struct的大小乘以元素的数量。

Normally when we say “object”, we refer to instances of a reference type, so the answer to your question is “yes”, but remember the difference and make sure that you don’t accidentally create a large array of a large struct.

通常,当我们说“对象”时,我们引用引用类型的实例,因此您的问题的答案是“是”,但请记住差异并确保您不会意外地创建大型结构的大型数组。

#2


5  

An array of reference types only contains references.

引用类型数组仅包含引用。

In a 32 bit application references are 32 bits (4 bytes), and in a 64 bit application references are 64 bits (8 bytes). So, you can calculate the approximate size by multiplying the array length with the reference size. (There are also a few extra bytes for internal variables for the array class, and some extra bytes are used for memory management.)

在32位应用程序中,引用是32位(4字节),而在64位应用程序中,引用是64位(8字节)。因此,您可以通过将数组长度乘以参考大小来计算近似大小。 (对于数组类,内部变量还有一些额外的字节,一些额外的字节用于内存管理。)

#3


2  

You can look at the memory occupied by an array using WinDBG + SOS (or PSSCOR2). IIRC, an array of reference types is represented in memory by its length, followed by references to its elements, i.e. it's exact size is PLATFORM_POINTER_SIZE * (array.Length + 1)

您可以使用WinDBG + SOS(或PSSCOR2)查看数组占用的内存。 IIRC,引用类型数组在内存中以其长度表示,然后引用其元素,即它的确切大小为PLATFORM_POINTER_SIZE *(array.Length + 1)

#4


0  

The array is made out of pointers (32bit or 64bit) that points to the objects. An object is a reference type, only value types are copied to the array itself.

该数组由指向对象的指针(32位或64位)组成。对象是引用类型,只有值类型被复制到数组本身。

#5


0  

As @Yves said it has references to the objects. The array is a block of memory as it as in C. So it size is sizeof(element) * count + the amount of memory needed by oop.

正如@Yves所说它有对象的引用。该数组是一个内存块,就像在C中一样。所以它的大小是sizeof(元素)* count + oop所需的内存量。