1

Looking at this, it appears that Single is the non integer number with the lowest memory requirements in C#. If I create:

Single[,] data = new Single[100000000, 10];

I am getting a:

System.OutOfMemoryException

Does this depend on my machine's available RAM? Could I keep an array like this or larger in memory? Thanks.

cs0815
  • 16,751
  • 45
  • 136
  • 299

1 Answers1

3

Does this depend on my machine's available RAM?

To an extent, yes. You cannot have an object that is significantly larger than your memory. However:

Could I keep an array like this or larger in memory?

32bit processes can be 2GB in size maximum (3GB with special exceptions). But even on 64bit systems using a 64bit process, you cannot exceed the size of 2 GB for a single .NET object. So you can have two large arrays of 1.5 GB each, but you could not have a single one of 3 GB. And yours seems to be way above that size.


As commenters pointed out, there is a way around this limit with later editions of .NET:

Put this in your App.config file:

<configuration>  
  <runtime>  
    <gcAllowVeryLargeObjects enabled="true" />  
  </runtime>  
</configuration>  
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 1
    Note that article you linked is 12 year old. Now there is a way to allocate objects larger than 2GB. – Evk Apr 05 '17 at 13:12
  • Well, you can use [``](https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx) to allow objects greater than 2GB in size. – Matthew Watson Apr 05 '17 at 13:13
  • @Evk Thanks, added it. – nvoigt Apr 05 '17 at 13:15
  • Wow this works for the example. i chose. Bit confused now how, as I definitely do not have 40GB of RAM ... must be magic. – cs0815 Apr 05 '17 at 13:21
  • @csetzkorn See [Virtual memory](https://en.wikipedia.org/wiki/Virtual_memory). Note that just declaring the array only reserves space for it in virtual memory. Only when you start setting values in it will the memory actually be commited. Try initialising it in a loop and watch your program slow to a crawl... ;) – Matthew Watson Apr 05 '17 at 13:24
  • @MatthewWatson - seeded it with random number and use NCalc against it work fairly fast. – cs0815 Apr 05 '17 at 13:25
  • @csetzkorn In my head, your array is only 3.8 GB. But I am known for not doing great calculating large numbers in my head, so better check it twice. – nvoigt Apr 05 '17 at 13:26
  • Yeah it's only 3GB. :) So it won't be very slow. If it really was 40GB then it would be significantly slower! – Matthew Watson Apr 05 '17 at 13:43