8
 var a = new double[7000,7000];

 FillValue(a,3);

It seems .Net doesn't actually allocate any memory to a after executing the first line. Only while it is running the FillValue call does it gradually eat the memory. (which is around 400MB)

Can anyone provide me with more details regarding it? I thought a is filled with 0 after default initialization, how could it take no memory at all?

4
  • 4
    How are you measuring whether or not it allocates memory? Commented Oct 23, 2012 at 21:17
  • @SethCarnegie sadly, I used a very crude way... just look at the windows task manager.. and set up break point, run a few times in a loop. It is 400MB memory per fill which is easy to spot. Commented Oct 23, 2012 at 21:20
  • 1
    This can also be because of the operating system. See "Memory Combining" blogs.msdn.com/b/b8/archive/2011/10/07/… Commented Oct 23, 2012 at 21:26
  • @ta.speot.is remember, that is relevant to Windows 8 only. I don't think it's germane to this discussion. Commented Oct 23, 2012 at 21:27

2 Answers 2

9

There are two ways to 'allocate memory' in Windows: to 'reserve' and to 'commit' memory.

The task manager only shows "Physical Memory Usage History"; the .NET VM apparently uses only reserved memory when you allocate an array, and then goes back and commits the parts that get used.

This way you can reserve memory without actually taking it up, which is more efficient, and reserving memory, according to MSDN, "reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk". That's why Task Manager doesn't show it.

You can read more about it on the VirtualAlloc page on MSDN.

This is an implementation detail though, so you shouldn't rely on it or anything. The Mono VM, for example, is likely to behave differently.

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

1 Comment

i am thinking if we can take advantage of it when working on sparse matrix manipulations.
7

Actually it allocates memory as you can see here:

http://ideone.com/Cd4BR1

Console.WriteLine("Memory before: {0}",GC.GetTotalMemory(true));
var a = new double[5000,5000];
Console.WriteLine("Memory after: {0}",GC.GetTotalMemory(true));

Memory before: 118784 
Memory after: 200224768

( needed to decrease the size of the array to prevent an OutOfMemoryException on ideone )

GC.GetTotalMemory Method

Retrieves the number of bytes currently thought to be allocated.

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.