The difference in memory usage between the Generic and Non-generic collections in .NET.

I read about collections in .NET these days. As you know, there are some advantages to using common collections compared to non-general ones : they are safe in type and there is no casting, no boxing / unpacking. This is why shared collections have a performance advantage.

If we consider that non-generic collections store each member as object, then we might think that generics also have a memory advantage. However, I did not find any information about the difference in memory usage.

Can someone clarify the issue?

+4
source share
3

, , , . . - ?

. ArrayList, int vs a List<int>. , 1000 int.

- ArrayList. ArrayList object[], 1000 int. List<int> int[], 1000 int s.

" "? . , . , , , , . , , - . , , , ; 2000 2000 ints.

, 1000.

, ? object[1000] 4000 32- 8000 64- , , . int[1000] 4000 . ( , , .)

, , , , . ?

, - . 4000 , 1000 ; . , .

object[] , ; - . ?

- , , , , . - , . , 32- .NET 8 16 64- . ; , , 4 int. , : 64- 8- , 4 64- .

: int[] 4 64- 32- . object[], 1000 , 16 32- 32 64- . , int[] vs a object[] 4 8 - .

, . . ?

, :

  • ,
  • ,

, :

  • ,
  • ,
  • ,
  • ,

, .

, .

, . 1000 , , , , , .. ; . , , .

, .

? List<string>?

, object[] a string[] . : (1) , (2) (3) .

+17

,

, . :

new ArrayList { 1, 2, 3 };

, object ( ), ArrayList. , , object, , , int.

, .

, . , . , (), , , .

, - .

: , , : . - . :

ArrayList a = new ArrayList { 1, 2, 3};

.

List<object> a = new List<object> { 1, 2, 3 };

, - . , object. .

:

ArrayList a = new ArrayList { myInstance, anotherInstance }

.

List<MyClass> a = new List<MyClass> { myInstance, anotherInstance }

. , , .

+1

, :

int valueType = 1;

, :

i = 1

, :

object boxingObject = valueType;

, , valueType value 1 :

boxingObject

1

, , Microsoft:

.

See the link for full details.

0
source

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


All Articles