Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - why doesn't .net allocate memory when initialize a 2d array?

 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...