none
When does dynamic memory allocation take place in C#? RRS feed

  • Question

  • When and how does dynamic memory allocation take place in C# Vs. static memory allocation?

    ArrayList list = new ArrayList()
    is a dynamic memory allocation as it does not specify the size of the arraylist?
    What happens in memory when dynamic memory allocation takes place versus when static memory allocation takes place?
    Tuesday, March 2, 2010 3:15 PM

Answers

  • Calle Mellergardh explained (quite well) the memory usage of the CLR -

    If you are asking about when memory is reserved for the ArrayList itself:

    When you construct an ArrayList with no specified size, an initial, static, empty array is set to the main list, and no actual memory is allocated.  When you construct an ArrayList with a specified initial size, an array of that length is allocated.

    The first time you call list.Add(...), the ArrayList reallocates it's internal list to an array of object references that's 4 elements long.  As you continue to Add(...) elements, any time you'd go over the size of the list (ie: when you add the 5th element), the internal array is allocated to an array 2x the current size (8, then 16, then 32, etc)...  The old, replaced array becomes elegible for GC.

    That being said, the current behavior is undocumented, and subject to change in a revision of the framework...

    Also, FYI: The generic List<T> class works in a similar fashion, and is recommended for usage instead of ArrayList if you're working in .NET 2+.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Tergiver Wednesday, March 3, 2010 4:37 PM
    • Marked as answer by SamAgain Friday, March 5, 2010 2:08 AM
    Tuesday, March 2, 2010 6:22 PM
    Moderator
  • Would'nt it be better to assign some size at the time of instantiation that is appropriate for the code and logic to keep it checking and assigning new memory as it runs out of it??
    myArrayList.Capacity = 40;

    Will you consider such memory assignment as dynamic memory allocation as it does assign memory in the process's reserved memory space?

    It's always better to use the constructor that allows you to specify an initial capacity, if you know, in advance, how big your list will most likely be:

    myArrayList = new ArrayList(40); // Set it here!

    If you use capacity, you'll construct with an initial size, then reinitialize with the capacity.

    (BTW -the size 16 detail you saw changes in different frameworks - in .NET 4, it initializes with a 0 size by using a static, empty object list....)



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Friday, March 5, 2010 2:08 AM
    Wednesday, March 3, 2010 9:47 PM
    Moderator

All replies

  • The .net runtime reserves memory for your application during startup. What happens when you declare new objects is that the GC optionally first performs a collection (reclaming unused memory) and then sets up a pointer for the new object in memory. So, memory allocation does not happen dynamically in the sense that more RAM is taken when ever you create new objects.

    /Calle
    - Still confused, but on a higher level -
    Tuesday, March 2, 2010 4:28 PM
  • Calle Mellergardh explained (quite well) the memory usage of the CLR -

    If you are asking about when memory is reserved for the ArrayList itself:

    When you construct an ArrayList with no specified size, an initial, static, empty array is set to the main list, and no actual memory is allocated.  When you construct an ArrayList with a specified initial size, an array of that length is allocated.

    The first time you call list.Add(...), the ArrayList reallocates it's internal list to an array of object references that's 4 elements long.  As you continue to Add(...) elements, any time you'd go over the size of the list (ie: when you add the 5th element), the internal array is allocated to an array 2x the current size (8, then 16, then 32, etc)...  The old, replaced array becomes elegible for GC.

    That being said, the current behavior is undocumented, and subject to change in a revision of the framework...

    Also, FYI: The generic List<T> class works in a similar fashion, and is recommended for usage instead of ArrayList if you're working in .NET 2+.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Tergiver Wednesday, March 3, 2010 4:37 PM
    • Marked as answer by SamAgain Friday, March 5, 2010 2:08 AM
    Tuesday, March 2, 2010 6:22 PM
    Moderator
  • @Reed:
       Your explanation is very informative to me. Could you tell me where you get this details? I need some more related reference. Many thanks.

    Wednesday, March 3, 2010 6:49 AM
  • @Reed:
       Your explanation is very informative to me. Could you tell me where you get this details? I need some more related reference. Many thanks.


    If you want internal implementation details, Rotor would be your best bet. Its available for download at the microsoft Site. It is nearly a production level code of All base class libraries and the CLR.
    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
    Wednesday, March 3, 2010 6:56 AM
  • @Reed:
       Your explanation is very informative to me. Could you tell me where you get this details? I need some more related reference. Many thanks.


    If you want internal implementation details, Rotor would be your best bet. Its available for download at the microsoft Site. It is nearly a production level code of All base class libraries and the CLR.
    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
    How can I view CLR translation of my C# code?
    Wednesday, March 3, 2010 3:31 PM
  • Calle Mellergardh explained (quite well) the memory usage of the CLR -

    If you are asking about when memory is reserved for the ArrayList itself:

    When you construct an ArrayList with no specified size, an initial, static, empty array is set to the main list, and no actual memory is allocated.  When you construct an ArrayList with a specified initial size, an array of that length is allocated.

    The first time you call list.Add(...), the ArrayList reallocates it's internal list to an array of object references that's 4 elements long.  As you continue to Add(...) elements, any time you'd go over the size of the list (ie: when you add the 5th element), the internal array is allocated to an array 2x the current size (8, then 16, then 32, etc)...  The old, replaced array becomes elegible for GC.

    That being said, the current behavior is undocumented, and subject to change in a revision of the framework...

    Also, FYI: The generic List<T> class works in a similar fashion, and is recommended for usage instead of ArrayList if you're working in .NET 2+.

    Reed Copsey, Jr. - http://reedcopsey.com
    Thanks,
    What is the "main list" ??
    Wednesday, March 3, 2010 3:32 PM
  • I'd get yourself a copy of .NET Reflector.  It will let you look at the "guts" of how your code is compiled, as well as look at library code, and see how it's written.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, March 3, 2010 5:30 PM
    Moderator
  • I'd get yourself a copy of .NET Reflector.  It will let you look at the "guts" of how your code is compiled, as well as look at library code, and see how it's written.
    Reed Copsey, Jr. - http://reedcopsey.com
    Thank you Reed, this is a very useful software. Instead of doing ArrayList.Contains, I now do .Equals because this is what this Contains() do anyways.

    However, for some of the .net base class library functions, I end up digging down to a level where it says
    "Specifies an internal call. An internal call is a call to a method implemented within the common language runtime itself."

    What does that mean? 
     
    Wednesday, March 3, 2010 9:14 PM
  • Calle Mellergardh explained (quite well) the memory usage of the CLR -

    If you are asking about when memory is reserved for the ArrayList itself:

    When you construct an ArrayList with no specified size, an initial, static, empty array is set to the main list, and no actual memory is allocated.  When you construct an ArrayList with a specified initial size, an array of that length is allocated.

    The first time you call list.Add(...), the ArrayList reallocates it's internal list to an array of object references that's 4 elements long.  As you continue to Add(...) elements, any time you'd go over the size of the list (ie: when you add the 5th element), the internal array is allocated to an array 2x the current size (8, then 16, then 32, etc)...  The old, replaced array becomes elegible for GC.

    That being said, the current behavior is undocumented, and subject to change in a revision of the framework...

    Also, FYI: The generic List<T> class works in a similar fashion, and is recommended for usage instead of ArrayList if you're working in .NET 2+.

    Reed Copsey, Jr. - http://reedcopsey.com
    It seems to assign size to 16 when you 
    ArrayList a = new ArrayList()
    and then as you mentioned, on every Add() if the size it up, it assigns twice as much.
    public ArrayList()
    {
        this._items = new object[0x10];
    }
    


    Would'nt it be better to assign some size at the time of instantiation that is appropriate for the code and logic to keep it checking and assigning new memory as it runs out of it??
    myArrayList.Capacity = 40;

    Will you consider such memory assignment as dynamic memory allocation as it does assign memory in the process's reserved memory space?
    Wednesday, March 3, 2010 9:37 PM
  • Would'nt it be better to assign some size at the time of instantiation that is appropriate for the code and logic to keep it checking and assigning new memory as it runs out of it??
    myArrayList.Capacity = 40;

    Will you consider such memory assignment as dynamic memory allocation as it does assign memory in the process's reserved memory space?

    It's always better to use the constructor that allows you to specify an initial capacity, if you know, in advance, how big your list will most likely be:

    myArrayList = new ArrayList(40); // Set it here!

    If you use capacity, you'll construct with an initial size, then reinitialize with the capacity.

    (BTW -the size 16 detail you saw changes in different frameworks - in .NET 4, it initializes with a 0 size by using a static, empty object list....)



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Friday, March 5, 2010 2:08 AM
    Wednesday, March 3, 2010 9:47 PM
    Moderator
  • ILDasm is a good tool to view IL code generated from .NET language compiler.

    Tuesday, March 9, 2010 4:12 PM