1

I want to clear concepts regarding Memory allocation of ArrayList vs Generic List, if both are value type and if both are reference type. Could any one hhelp to clear out?

1
  • 1
    what is your concern about Memory Allocation? just curious. Commented Sep 26, 2012 at 17:56

3 Answers 3

2

The only difference in memory use is when you store a Value type. The ArrayList will have to Box (copy) the value. A boxed value will be placed on the Heap, consuming at least an extra header block (ca 20 bytes).

But this will only be significant when you store many millions of items, not something you do all the time.

Sign up to request clarification or add additional context in comments.

2 Comments

Array List need boxing when storing value type thats why it consume more memory and is not purely reference type.is it?
The ArrayList itself is still a pure reference type.
1

They are both reference types. The only difference is that ArrayList is weakly typed. Value types such as int, bool etc that are stored in it are boxed into the object type. Then, you unbox them when you cast each item in the ArrayList.

Because everything is boxed into an object, you can store objects of different types in an ArrayList.

Generic List is strongly typed, that is, it can store objects of the same type. There's no boxing, so it's more efficient.

The boxing process allocates more memory to encapsulate the object into the weak type object.

If you stored only objects of reference types in the ArrayList, then boxing is not used, rather another mechanism is used called reference conversion.

1 Comment

Boxing only happens for Value types, your answer is not very clear about that.
0
  • ArrayList is a Reference Type,but not Typesafe and less efficient
  • List<T> or Generic list is a Reference Type,but is Type Safe and efficient

Here is the SO post on Memory Allocation of Reference Types How memory is allocated to reference types in C#?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.