-1

If I have a class with an array that is not initialized how much memory would that use?

Example code:

public class myClass
{
    int x;
    public objClass[] objArr;
}

public class objClass
{
    int y;
    int z;
}

 myClass m0 = new myClass();
 //vs
 myClass m1 = new myClass();
 m1.objArr = new objClass[0];
 //vs
 myClass m2 = new myClass();
 m2.objArr = new objClass[2];

Would m0 use less memory than m1 and m2?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
  • 3
    The memory of the class is the same as the array is just a reference, but when you initialize the array then there is more memory allocated on the heap. – juharr Jul 18 '18 at 20:55
  • If you care about memory consumption you might want to use a different framework, one that doesn't manege the memory for you. – Zohar Peled Jul 18 '18 at 20:56
  • @juharr so the reference of the uninitialized array would still use 64bit even if it is not referencing / pointing to anything ? – Patrik Fröhler Jul 18 '18 at 20:58
  • 2
    @PatrikFröhler Yes it will be a `null` reference and would take up 64 or 32 bits depending on the machine within the memory of the object. The actually array memory would be separate. – juharr Jul 18 '18 at 21:00
  • 2
    The array does not take extra space in a myClass object, the objArr member is a *reference* to the array. It always takes 4 or 8 bytes. Once you initialize it the array starts taking its own space on the GC heap, proportional to the number of elements. – Hans Passant Jul 18 '18 at 21:37

2 Answers2

1

If I have a class with an array that is not initialized how much memory would that use?

Yes.

if you do not initialize the array then you will end up with a pointer pointing to 0x0 (null) which uses 4 bytes to store the pointer.

if you initialize it then it uses that 4 bytes pointer + space for that array.

But this should really be the least of your concern until you run into memory consumption issues. .NET will take care of memory management for ya.

Steve
  • 11,696
  • 7
  • 43
  • 81
  • 1
    That's not a yes/no question. Also should note that the space for the array will be separate from the memory used to store the object data. – juharr Jul 18 '18 at 21:02
  • 4
    "which uses 4 bytes to store the pointer." - on 32-bit, which is increasingly rare - on x64 it is 8 bytes – Marc Gravell Jul 18 '18 at 21:35
0

Since this is most likely to be under Windows which is demand-paged virtual memory operating system with your code you simply reserved virtual memory. If you monitor you process within your Task Manager and enable "Commit size" column you can see how much you reserved.

So

If I have a class with an array that is not initialized how much memory would that use?

It will not use any memory it will just commit the size of the pointer plus the size of the array, but only commit, once you first initiate i then your operating system will allocate this memory for you.

kuskmen
  • 3,648
  • 4
  • 27
  • 54
  • The .NET runtime will null out the memory for any given class. This will lead to an increase of your working set. It is not true that this only increases commit or reserve size. The memory used by the GC is fully soft faulted memory contributing to your active working set. – Alois Kraus Jul 18 '18 at 21:42
  • Hmm it appears, Hans already answered this here: https://stackoverflow.com/questions/49333416/unmanaged-memory-not-showing-up-in-task-manager/49334609#49334609 are he is speaking of another case? – kuskmen Jul 18 '18 at 21:44
  • @AloisKraus, I am not sure I understand, can you elaborate on what does "The memory used by the GC is fully soft faulted memory contributing to your active working set." mean, thanks – kuskmen Jul 18 '18 at 21:45
  • This is a special case for arrays. If you new up e.g. 100 million Program instances and store these in an array to prevent GC you will allocate and touch that memory. Each new class instance will contribute to your working set. Try it out for yourself. If you add now a few more members to your class layout the memory consumption will rise. – Alois Kraus Jul 18 '18 at 21:52
  • Regarding your question: The CLR guarantees that each allocated class has a default value of 0 for all members. Since the GC memory is reused this means it has to null everything out before handing to you the newly allocated class reference. That means the CLR needs to touch that memory and you are getting an increase in the working set. – Alois Kraus Jul 18 '18 at 21:58